Bird
Raised Fist0
PHPprogramming~20 mins

Named arguments in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Named Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of function call with named arguments
What is the output of this PHP code using named arguments?
PHP
<?php
function greet($name, $greeting = 'Hello') {
    return "$greeting, $name!";
}
echo greet(greeting: 'Hi', name: 'Alice');
?>
AHi, Alice!
BSyntaxError
CHello, Alice!
DHi, greeting!
Attempts:
2 left
💡 Hint
Named arguments allow you to specify parameters by name in any order.
❓ Predict Output
intermediate
2:00remaining
Named arguments with default values
What will this PHP code output when using named arguments?
PHP
<?php
function calculate($x, $y = 10, $z = 5) {
    return $x + $y * $z;
}
echo calculate(z: 3, x: 2);
?>
A17
BSyntaxError
C32
D15
Attempts:
2 left
💡 Hint
Remember the order of operations and default values.
❓ Predict Output
advanced
2:00remaining
Named arguments with variadic function
What is the output of this PHP code using named arguments with a variadic function?
PHP
<?php
function sum($a, ...$numbers) {
    return $a + array_sum($numbers);
}
echo sum(numbers: [2, 3, 4], a: 1);
?>
AFatal error
B1
CWarning: Missing argument for variadic parameter
D10
Attempts:
2 left
💡 Hint
Variadic parameters cannot be passed as named arguments as arrays directly.
❓ Predict Output
advanced
2:00remaining
Named arguments with type mismatch
What error does this PHP code produce when calling a function with named arguments?
PHP
<?php
function multiply(int $a, int $b) {
    return $a * $b;
}
echo multiply(a: 5, b: 'three');
?>
A15
BTypeError
CWarning: Invalid argument
D5three
Attempts:
2 left
💡 Hint
PHP 7+ enforces type declarations strictly by default.
🧠 Conceptual
expert
2:00remaining
Behavior of named arguments with inheritance
Consider this PHP code with inheritance and named arguments. What is the output?
PHP
<?php
class Base {
    public function display($x, $y = 10) {
        echo $x + $y;
    }
}
class Child extends Base {
    public function display($x, $y = 20) {
        echo $x * $y;
    }
}
$obj = new Child();
$obj->display(x: 3, y: 4);
?>
A12
B7
CFatal error
D60
Attempts:
2 left
💡 Hint
Check method signatures and parameter order in inheritance.

Practice

(1/5)
1.

What is the main benefit of using named arguments in PHP functions?

easy
A. Named arguments make functions run faster.
B. You must always provide all arguments in order.
C. You can specify arguments by name and change their order.
D. They allow you to define new functions inside existing ones.

Solution

  1. Step 1: Understand named arguments concept

    Named arguments let you specify function parameters by their names instead of position.
  2. Step 2: Identify the benefit

    This allows changing the order of arguments and skipping optional ones, improving readability.
  3. Final Answer:

    You can specify arguments by name and change their order. -> Option C
  4. Quick Check:

    Named arguments = specify by name and reorder [OK]
Hint: Named arguments let you reorder parameters by name [OK]
Common Mistakes:
  • Thinking named arguments speed up execution
  • Believing all arguments must be given in order
  • Confusing named arguments with function definitions
2.

Which of the following is the correct syntax to call a function greet with named arguments name and age?

function greet($name, $age) { return "Hello $name, you are $age."; }
easy
A. greet("Alice", age: 30);
B. greet(name = "Alice", age = 30);
C. greet(age => 30, name => "Alice");
D. greet(name: "Alice", age: 30);

Solution

  1. Step 1: Recall named argument syntax in PHP

    Named arguments use the syntax paramName: value when calling functions.
  2. Step 2: Check each option

    greet(name: "Alice", age: 30); uses name: "Alice" and age: 30, which is correct syntax.
  3. Final Answer:

    greet(name: "Alice", age: 30); -> Option D
  4. Quick Check:

    Named argument syntax = paramName: value [OK]
Hint: Use colon : for named arguments in calls [OK]
Common Mistakes:
  • Using => instead of : for named arguments
  • Using = instead of : in calls
  • Mixing positional and named arguments incorrectly
3.

What will be the output of the following PHP code?

function describe($color, $shape) {
    return "A $color $shape.";
}
echo describe(shape: "circle", color: "red");
medium
A. A red circle.
B. A red.
C. Syntax error.
D. A circle red.

Solution

  1. Step 1: Understand named argument order

    Named arguments allow changing the order of parameters when calling the function.
  2. Step 2: Substitute values in function

    Here, shape is "circle" and color is "red". The return string is "A $color $shape.", so it becomes "A red circle.".
  3. Final Answer:

    A red circle. -> Option A
  4. Quick Check:

    Named arguments reorder parameters = "A red circle." [OK]
Hint: Named arguments reorder parameters without error [OK]
Common Mistakes:
  • Assuming order must match function definition
  • Expecting syntax error from reordered named arguments
  • Mixing up parameter names and values
4.

Identify the error in the following PHP code using named arguments:

function multiply($x, $y) {
    return $x * $y;
}
echo multiply(y: 5, 3);
medium
A. Function multiply is undefined.
B. Positional argument after named argument causes error.
C. Missing colon in named argument syntax.
D. No error; code runs fine.

Solution

  1. Step 1: Check argument order rules

    In PHP, positional arguments must come before named arguments when calling a function.
  2. Step 2: Analyze the call

    The call uses a named argument y: 5 first, then a positional argument 3, which is invalid syntax.
  3. Final Answer:

    Positional argument after named argument causes error. -> Option B
  4. Quick Check:

    Positional args must precede named args [OK]
Hint: Positional args must come before named args [OK]
Common Mistakes:
  • Placing positional arguments after named arguments
  • Forgetting colon in named argument
  • Assuming named arguments can be mixed in any order
5.

Given the function below, which call correctly uses named arguments to skip the optional country parameter and specify city only?

function locationInfo(string $city, string $country = "USA") {
    return "$city, $country";
}
hard
A. locationInfo(city: "Boston");
B. locationInfo("Boston", country: "Canada");
C. locationInfo(city: "Boston", country: "Canada");
D. locationInfo(country: "Canada", city: "Boston");

Solution

  1. Step 1: Understand optional parameters and named arguments

    The country parameter has a default value, so it can be skipped.
  2. Step 2: Identify call that skips country and sets city

    locationInfo(city: "Boston"); calls locationInfo(city: "Boston"), skipping country and using default "USA".
  3. Final Answer:

    locationInfo(city: "Boston"); -> Option A
  4. Quick Check:

    Skip optional params by omitting them in named args [OK]
Hint: Skip optional params by not naming them [OK]
Common Mistakes:
  • Trying to skip required parameters
  • Providing positional argument after named argument
  • Specifying optional parameter unnecessarily
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