0
0
PHPprogramming~5 mins

Default parameter values in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afunction example($param) = 10 {}
Bfunction example($param = 10) {}
Cfunction example($param: 10) {}
Dfunction example($param <- 10) {}
What happens if you call a function with a default parameter but provide an argument?
AThe function ignores the argument and uses the default.
BThe function throws an error.
CThe default value overrides the argument.
DThe default value is ignored and the provided argument is used.
Which of these parameter orders is valid in PHP?
ABoth B and D
Bfunction test($a, $b = 2) {}
Cfunction test($a = 1, $b) {}
Dfunction test($a = 1, $b = 2) {}
What is the output of this code?<br>
function greet($name = "Guest") {
  echo "Hello, $name!";
}
greet("Anna");
AHello, Anna!
BHello, Guest!
CHello, !
DError
Can default parameter values be expressions like function calls in PHP?
AYes, but only if the function is declared as static.
BYes, any expression is allowed.
CNo, only constant values are allowed.
DNo, default values must be null.
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.