0
0
PHPprogramming~10 mins

Default parameter values 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 set a default value for the parameter.

PHP
<?php
function greet($name = [1]) {
    echo "Hello, $name!";
}
greet();
?>
Drag options to blanks, or click blank then click option'
A"Guest"
Bnull
C0
D""
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string default value.
Using null without handling it inside the function.
2fill in blank
medium

Complete the code to call the function with the default parameter.

PHP
<?php
function sayAge($age = [1]) {
    echo "Age is $age.";
}
sayAge();
?>
Drag options to blanks, or click blank then click option'
A18
Bnull
C0
D21
Attempts:
3 left
💡 Hint
Common Mistakes
Using null as default without checking inside the function.
Passing a string instead of a number.
3fill in blank
hard

Fix the error in the function definition by completing the default parameter value.

PHP
<?php
function multiply($num, $factor = [1]) {
    return $num * $factor;
}
echo multiply(5);
?>
Drag options to blanks, or click blank then click option'
A"2"
Bnull
C2
D"two"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around numbers for default values.
Using null without handling it.
4fill in blank
hard

Fill both blanks to create a function with two parameters, one with a default value.

PHP
<?php
function describe($item, $color = [1]) {
    echo "The $item is $color.";
}
describe([2]);
?>
Drag options to blanks, or click blank then click option'
A"red"
B"apple"
C"banana"
D"blue"
Attempts:
3 left
💡 Hint
Common Mistakes
Not passing the required first argument.
Using a non-string default value for color.
5fill in blank
hard

Fill all three blanks to define a function with default parameters and call it correctly.

PHP
<?php
function order($food = [1], $drink = [2]) {
    echo "Order: $food and $drink.";
}
order([3]);
?>
Drag options to blanks, or click blank then click option'
A"pizza"
B"water"
C"burger", "juice"
D"burger"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing multiple arguments as one string.
Not matching the number of arguments with parameters.