Recall & Review
beginner
What are default parameter values in PHP functions?
Default parameter values allow a function to have predefined values for parameters if no argument is passed when the function is called.
Click to reveal answer
beginner
How do you define a default parameter value in a PHP function?
You assign a value to the parameter in the function declaration, like: <code>function greet($name = "Guest") { ... }</code>.Click to reveal answer
beginner
What happens if you call a PHP function without passing an argument for a parameter that has a default value?
The function uses the default value defined for that parameter.
Click to reveal answer
intermediate
Can parameters with default values be placed before parameters without default values in PHP?
No, parameters with default values must come after parameters without default values to avoid syntax errors.
Click to reveal answer
beginner
Example: What will this PHP code output?<br><pre>function sayHi($name = "Friend") {
echo "Hi, $name!";
}
sayHi();</pre>It will output:
Hi, Friend! because the function uses the default value for $name when no argument is passed.Click to reveal answer
What is the correct way to set a default value for a parameter in PHP?
✗ Incorrect
In PHP, default values are assigned using the equals sign inside the function parameter list, like
$param = 10.What happens if you call a function with a default parameter but provide an argument?
✗ Incorrect
When an argument is provided, PHP uses that argument instead of the default value.
Which of these parameter orders is valid in PHP?
✗ Incorrect
Parameters without default values must come first. So B and D are valid, A is invalid.
What is the output of this code?<br>
function greet($name = "Guest") {
echo "Hello, $name!";
}
greet("Anna");✗ Incorrect
Since an argument "Anna" is passed, the function uses it instead of the default.
Can default parameter values be expressions like function calls in PHP?
✗ Incorrect
PHP requires default parameter values to be constant expressions, not function calls or variables.
Explain how default parameter values work in PHP functions and why they are useful.
Think about what happens when you call a function without all arguments.
You got /3 concepts.
Describe the rules about the order of parameters with and without default values in PHP function declarations.
Consider what happens if you put a default parameter before a required one.
You got /3 concepts.