Bird
Raised Fist0
PHPprogramming~10 mins

Type declarations for parameters in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the parameter type as integer.

PHP
<?php
function addOne([1] $num) {
    return $num + 1;
}
?>
Drag options to blanks, or click blank then click option'
Abool
Bint
Cfloat
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' instead of 'int' for a number parameter.
Forgetting to declare the type at all.
2fill in blank
medium

Complete the code to declare the parameter type as string.

PHP
<?php
function greet([1] $name) {
    return "Hello, " . $name;
}
?>
Drag options to blanks, or click blank then click option'
Astring
Bbool
Carray
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' instead of 'string' for a text parameter.
Not declaring any type, which allows any type.
3fill in blank
hard

Fix the error in the function parameter type declaration.

PHP
<?php
function multiply([1] $value1, int $value2) {
    return $value1 * $value2;
}
?>
Drag options to blanks, or click blank then click option'
Anumber
Bfloat
Cinteger
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'integer' instead of 'int' causes a syntax error.
Using 'number' which is not a valid PHP type.
4fill in blank
hard

Fill both blanks to declare parameter types for a function that takes a float and a boolean.

PHP
<?php
function checkValues([1] $score, [2] $passed) {
    if ($passed) {
        return $score;
    }
    return 0;
}
?>
Drag options to blanks, or click blank then click option'
Afloat
Bbool
Cint
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' instead of 'float' for decimal numbers.
Using 'string' instead of 'bool' for true/false.
5fill in blank
hard

Fill all three blanks to declare parameter types for a function that takes a string, an integer, and a boolean.

PHP
<?php
function processData([1] $name, [2] $age, [3] $isMember) {
    if ($isMember) {
        return "Welcome " . $name . ", age " . $age;
    }
    return "Guest";
}
?>
Drag options to blanks, or click blank then click option'
Astring
Bint
Cbool
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up types like using 'float' for age.
Not using 'bool' for true/false parameters.

Practice

(1/5)
1. What does a type declaration for a function parameter in PHP do?
easy
A. It tells PHP what kind of data the function expects for that parameter.
B. It changes the function's return type automatically.
C. It makes the function run faster by optimizing memory.
D. It hides the parameter from being accessed outside the function.

Solution

  1. Step 1: Understand the purpose of type declarations

    Type declarations specify the expected data type for function parameters, helping PHP check inputs.
  2. Step 2: Identify what type declarations do not do

    They do not change return types, optimize speed, or hide parameters.
  3. Final Answer:

    It tells PHP what kind of data the function expects for that parameter. -> Option A
  4. Quick Check:

    Type declarations = specify expected parameter type [OK]
Hint: Type declarations specify expected input types for functions [OK]
Common Mistakes:
  • Thinking type declarations change return types
  • Believing they improve speed directly
  • Confusing parameter visibility with type declarations
2. Which of the following is the correct syntax to declare a function parameter as an integer in PHP?
easy
A. function add(integer $a, integer $b) {}
B. function add($int a, $int b) {}
C. function add(int: $a, int: $b) {}
D. function add(int $a, int $b) {}

Solution

  1. Step 1: Recall PHP type declaration syntax

    PHP uses the type name followed by the parameter with a $ sign, like int $a.
  2. Step 2: Check each option's syntax

    function add(int $a, int $b) {} matches correct syntax. Options A, B, and D use invalid syntax.
  3. Final Answer:

    function add(int $a, int $b) {} -> Option D
  4. Quick Check:

    Correct syntax = type before $parameter [OK]
Hint: Type before $parameter name, no extra symbols [OK]
Common Mistakes:
  • Putting $ before type name
  • Using 'integer' instead of 'int'
  • Adding colons or extra symbols
3. What will be the output of this PHP code?
function multiply(float $x, float $y) {
    return $x * $y;
}
echo multiply(3, 4.5);
medium
A. 13.5
B. 12
C. TypeError
D. 7.5

Solution

  1. Step 1: Understand parameter types and argument passing

    The function expects floats. PHP will convert integer 3 to float 3.0 automatically.
  2. Step 2: Calculate the multiplication

    3.0 * 4.5 = 13.5, so the function returns 13.5.
  3. Final Answer:

    13.5 -> Option A
  4. Quick Check:

    3 * 4.5 = 13.5 [OK]
Hint: PHP auto-converts int to float if declared [OK]
Common Mistakes:
  • Expecting TypeError for int passed to float
  • Calculating wrong multiplication
  • Confusing output with integer result
4. Identify the error in this PHP function declaration:
function greet(string name) {
    return "Hello, $name!";
}
medium
A. Wrong type declaration keyword
B. Function must return int
C. Missing $ before parameter name
D. No error, code is correct

Solution

  1. Step 1: Check parameter syntax

    In PHP, parameter names must start with a $ sign, so string name is incorrect.
  2. Step 2: Identify the correct syntax

    The correct syntax is string $name with $ before the parameter name.
  3. Final Answer:

    Missing $ before parameter name -> Option C
  4. Quick Check:

    Parameter names need $ [OK]
Hint: Parameter names always start with $ in PHP [OK]
Common Mistakes:
  • Omitting $ before parameter name
  • Confusing type declaration with variable name
  • Assuming no $ needed in function parameters
5. Given this function:
function process(array $data, int $limit = 10) {
    return array_slice($data, 0, $limit);
}

Which call will cause a TypeError?
hard
A. process([1, 2, 3], 2)
B. process('not an array', 5)
C. process([1, 2, 3])
D. process([1, 2, 3], 0)

Solution

  1. Step 1: Analyze parameter types and defaults

    The first parameter must be an array; the second is an int with default 10.
  2. Step 2: Check each call

    process([1, 2, 3], 2) passes array and int correctly. process('not an array', 5) passes a string instead of array, causing TypeError. Options A, B, and C pass valid types.
  3. Final Answer:

    process('not an array', 5) causes TypeError -> Option B
  4. Quick Check:

    First param must be array, string causes error [OK]
Hint: Check argument types match declared parameter types [OK]
Common Mistakes:
  • Passing wrong type for array parameter
  • Assuming default values fix type errors
  • Ignoring parameter order and types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard