Bird
Raised Fist0
PHPprogramming~20 mins

Default parameter values in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Default Parameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of function with default parameter
What is the output of this PHP code?
PHP
<?php
function greet($name = "Guest") {
    return "Hello, $name!";
}
echo greet();
?>
AHello, Guest!
BHello, !
CHello, name!
DError: Missing argument
Attempts:
2 left
💡 Hint
Check what happens when you call the function without any argument.
❓ Predict Output
intermediate
2:00remaining
Default parameter with multiple arguments
What is the output of this PHP code?
PHP
<?php
function add($a, $b = 10) {
    return $a + $b;
}
echo add(5);
?>
A15
BError: Missing argument
C5
D10
Attempts:
2 left
💡 Hint
Look at the default value of the second parameter.
❓ Predict Output
advanced
2:00remaining
Default parameter with expression
What is the output of this PHP code?
PHP
<?php
function multiply($x, $y = 2 * 3) {
    return $x * $y;
}
echo multiply(4);
?>
A8
B24
CError: Default value must be a constant expression
D12
Attempts:
2 left
💡 Hint
Default parameters can be expressions evaluated at compile time.
❓ Predict Output
advanced
2:00remaining
Default parameter with null and type hint
What is the output of this PHP code?
PHP
<?php
function welcome(string $name = null) {
    if ($name === null) {
        return "Welcome, Guest!";
    }
    return "Welcome, $name!";
}
echo welcome();
?>
AWelcome, null!
BWelcome, !
CTypeError: Argument 1 passed must be of the type string, null given
DWelcome, Guest!
Attempts:
2 left
💡 Hint
Type hints apply to default values; null does not match string.
❓ Predict Output
expert
3:00remaining
Default parameter with mutable object
What is the output of this PHP code?
PHP
<?php
function addItem(array &$arr = []) {
    $arr[] = "item";
    return $arr;
}

$a = addItem();
$b = addItem();

print_r($a);
print_r($b);
?>
A
Array ( [0] =&gt; item )
Array ( [0] =&gt; item )
B
Array ( [0] =&gt; item [1] =&gt; item )
Array ( [0] =&gt; item [1] =&gt; item )
C
Array ( [0] =&gt; item )
Array ( [0] =&gt; item [1] =&gt; item )
DError: Default value for parameters with reference must be null
Attempts:
2 left
💡 Hint
Check if default values can be references in PHP.

Practice

(1/5)
1. What is the purpose of default parameter values in PHP functions?
easy
A. To force the user to always pass an argument
B. To make the function run faster
C. To provide a value if no argument is passed when calling the function
D. To declare a variable inside the function

Solution

  1. Step 1: Understand default parameters

    Default parameters give a preset value to a function argument if no value is provided during the call.
  2. Step 2: Identify the purpose

    This helps avoid errors and makes the function flexible by not requiring all arguments every time.
  3. Final Answer:

    To provide a value if no argument is passed when calling the function -> Option C
  4. Quick Check:

    Default parameter = fallback value [OK]
Hint: Default values fill in missing arguments automatically [OK]
Common Mistakes:
  • Thinking default values speed up the function
  • Believing default values force argument passing
  • Confusing default parameters with variable declarations
2. Which of the following is the correct syntax to define a function with a default parameter value in PHP?
easy
A. function greet($name = "Guest") { echo "Hello, $name!"; }
B. function greet($name) = "Guest" { echo "Hello, $name!"; }
C. function greet($name: "Guest") { echo "Hello, $name!"; }
D. function greet($name) { $name = "Guest"; echo "Hello, $name!"; }

Solution

  1. Step 1: Review PHP default parameter syntax

    In PHP, default values are assigned using the equals sign inside the function parameter list.
  2. Step 2: Check each option

    function greet($name = "Guest") { echo "Hello, $name!"; } uses correct syntax: $name = "Guest" inside parentheses. Syntaxes using = after the closing parenthesis or colon (:) are invalid. function greet($name) { $name = "Guest"; echo "Hello, $name!"; } assigns default inside the function body, not as a parameter default.
  3. Final Answer:

    function greet($name = "Guest") { echo "Hello, $name!"; } -> Option A
  4. Quick Check:

    Default parameter = equals sign in parentheses [OK]
Hint: Use = inside parentheses to set default values [OK]
Common Mistakes:
  • Using colon or other symbols instead of =
  • Assigning default inside function body instead of parameter list
  • Placing default value outside parentheses
3. What will be the output of the following PHP code?
function multiply($a, $b = 2) {
  return $a * $b;
}
echo multiply(5);
medium
A. 7
B. 5
C. Error: Missing argument
D. 10

Solution

  1. Step 1: Analyze function call with one argument

    The function multiply expects two parameters, but the second has a default value of 2.
  2. Step 2: Calculate the result

    Calling multiply(5) uses $a=5 and default $b=2, so 5 * 2 = 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    5 * default 2 = 10 [OK]
Hint: If argument missing, default value is used [OK]
Common Mistakes:
  • Expecting error for missing second argument
  • Using wrong default value
  • Confusing output with sum instead of product
4. Identify the error in this PHP function definition:
function greet($name = "Guest", $age) {
  echo "Hello $name, age $age";
}
medium
A. Function cannot have default parameters
B. Default parameter must be last or followed only by default parameters
C. Missing semicolon after echo statement
D. Variable names cannot have default values

Solution

  1. Step 1: Check parameter order rules

    In PHP, parameters with default values must come after parameters without defaults.
  2. Step 2: Identify the error

    Here, $name has a default but $age does not, and $age comes after $name, which is invalid syntax.
  3. Final Answer:

    Default parameter must be last or followed only by default parameters -> Option B
  4. Quick Check:

    Default parameters must be at the end [OK]
Hint: Place all default parameters after non-default ones [OK]
Common Mistakes:
  • Putting default parameters before required ones
  • Ignoring PHP syntax rules for parameter order
  • Thinking semicolon or variable name causes error
5. Consider this PHP function:
function createUser($name, $role = "user", $active = true) {
  return "$name is a $role and active status is " . ($active ? "yes" : "no");
}

// Call the function
$result = createUser("Alice", active: false);
echo $result;

What will be the output and why?
hard
A. Alice is a user and active status is no
B. Alice is a user and active status is yes
C. Syntax error due to incorrect argument order
D. Error: Named arguments not supported

Solution

  1. Step 1: Understand named arguments and defaults

    PHP 8+ supports named arguments, so calling createUser("Alice", active: false) sets $active to false and uses default $role = "user".
  2. Step 2: Evaluate function output

    $name = "Alice", $role = "user", $active = false, so output is "Alice is a user and active status is no".
  3. Final Answer:

    Alice is a user and active status is no -> Option A
  4. Quick Check:

    Named argument sets active=false, role defaults [OK]
Hint: Named arguments can skip defaults, set specific params [OK]
Common Mistakes:
  • Assuming named arguments are unsupported in PHP
  • Expecting syntax error due to argument order
  • Confusing default parameter usage with named arguments
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