0
0
PHPprogramming~10 mins

Type declarations for parameters in PHP - Interactive Code Practice

Choose your learning style9 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.