Bird
Raised Fist0
PHPprogramming~20 mins

Anonymous function syntax in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Anonymous Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of an anonymous function assigned to a variable
What is the output of this PHP code?
PHP
<?php
$greet = function($name) {
    return "Hello, $name!";
};
echo $greet("Alice");
?>
AError: Undefined variable $name
BHello, $name!
CHello, Alice!
DHello, !
Attempts:
2 left
💡 Hint
Look at how the anonymous function uses the parameter inside the string.
❓ Predict Output
intermediate
2:00remaining
Using 'use' keyword to access external variable
What will this PHP code output?
PHP
<?php
$message = "Hi";
$func = function($name) use ($message) {
    return "$message, $name!";
};
echo $func("Bob");
?>
AHi, $name!
BHi, Bob!
CError: Undefined variable $message
DHi!
Attempts:
2 left
💡 Hint
Check how the 'use' keyword brings $message inside the function.
🔧 Debug
advanced
2:00remaining
Identify the error in anonymous function syntax
What error does this PHP code produce?
PHP
<?php
$func = function($x) {
    return $x * 2
};
echo $func(5);
?>
AParse error: syntax error, unexpected '}', expecting ';'
BFatal error: Call to undefined function function()
CWarning: Missing argument for function
DNo error, outputs 10
Attempts:
2 left
💡 Hint
Check the line before the closing brace for missing punctuation.
❓ Predict Output
advanced
2:00remaining
Anonymous function modifying external variable by reference
What is the output of this PHP code?
PHP
<?php
$count = 0;
$increment = function() use (&$count) {
    $count++;
};
$increment();
$increment();
echo $count;
?>
A0
BError: Cannot use variable by reference
C1
D2
Attempts:
2 left
💡 Hint
Look at how the variable is passed by reference with &.
🧠 Conceptual
expert
3:00remaining
Understanding closure binding in anonymous functions
Given this PHP code, what will be the output?
PHP
<?php
class Counter {
    private int $count = 0;
    public function getIncrementer() {
        return function() {
            $this->count++;
            return $this->count;
        };
    }
}

$counter = new Counter();
$inc = $counter->getIncrementer();
echo $inc();
echo $inc();
?>
A12
B01
CError: Using $this in anonymous function without binding
D11
Attempts:
2 left
💡 Hint
Anonymous functions inside classes can access $this if called correctly.

Practice

(1/5)
1. What is an anonymous function in PHP?
easy
A. A function that is automatically called on page load
B. A function that must always have a name
C. A function without a name, often assigned to a variable
D. A function that can only be used inside classes

Solution

  1. Step 1: Understand function naming

    Anonymous functions do not have a name, unlike regular functions.
  2. Step 2: Recognize assignment to variables

    They are often assigned to variables for later use or callbacks.
  3. Final Answer:

    A function without a name, often assigned to a variable -> Option C
  4. Quick Check:

    Anonymous function = unnamed function assigned to variable [OK]
Hint: Anonymous means no name, usually stored in a variable [OK]
Common Mistakes:
  • Thinking anonymous functions must have names
  • Confusing anonymous functions with class methods
  • Assuming anonymous functions run automatically
2. Which of the following is the correct syntax to create an anonymous function in PHP?
easy
A. $func = anonymous function() { echo 'Hello'; };
B. $func = function() { echo 'Hello'; };
C. function $func() { echo 'Hello'; };
D. $func := function() { echo 'Hello'; };

Solution

  1. Step 1: Identify correct anonymous function syntax

    In PHP, anonymous functions use the keyword function without a name and are assigned to variables.
  2. Step 2: Check each option

    $func = function() { echo 'Hello'; }; uses correct syntax: $func = function() { ... };. Others have invalid keywords or syntax.
  3. Final Answer:

    $func = function() { echo 'Hello'; }; -> Option B
  4. Quick Check:

    Anonymous function syntax = function keyword + no name + assignment [OK]
Hint: Use 'function' keyword without name, assign to variable [OK]
Common Mistakes:
  • Adding 'anonymous' keyword before function
  • Using := instead of = for assignment
  • Trying to name the anonymous function
3. What will be the output of the following PHP code?
$x = 5;
$func = function() use ($x) {
echo $x * 2;
};
$func();
medium
A. 5
B. Error: Undefined variable
C. 0
D. 10

Solution

  1. Step 1: Understand 'use' keyword in anonymous functions

    The use ($x) imports the variable $x from the outer scope into the anonymous function.
  2. Step 2: Calculate the output

    The function echoes $x * 2, which is 5 * 2 = 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    use imports variable, output = 5*2 = 10 [OK]
Hint: Remember 'use' imports outside variables into function [OK]
Common Mistakes:
  • Forgetting to use 'use' and expecting access to $x
  • Expecting output 5 instead of 10
  • Assuming variable is undefined inside function
4. Identify the error in the following PHP code:
$x = 10;
$func = function() {
echo $x;
};
$func();
medium
A. Variable $x is not accessible inside the anonymous function
B. Missing semicolon after function definition
C. Anonymous functions cannot echo variables
D. Function must have a name

Solution

  1. Step 1: Check variable scope inside anonymous function

    Variables outside anonymous functions are not accessible inside unless imported with use.
  2. Step 2: Identify the error cause

    The code tries to echo $x inside the function without use ($x), causing an undefined variable error.
  3. Final Answer:

    Variable $x is not accessible inside the anonymous function -> Option A
  4. Quick Check:

    Outside variables need 'use' to be accessed inside [OK]
Hint: Use 'use' to access outside variables inside anonymous functions [OK]
Common Mistakes:
  • Assuming outside variables are automatically accessible
  • Thinking anonymous functions must have names
  • Confusing syntax errors with scope errors
5. How can you modify this PHP code to correctly print the sum of two variables using an anonymous function?
$a = 3;
$b = 4;
$sumFunc = function() {
echo $a + $b;
};
$sumFunc();
hard
A. Both A and B are correct ways
B. $sumFunc = function($a, $b) { echo $a + $b; }; $sumFunc($a, $b);
C. $sumFunc = function() use ($a, $b) { echo $a + $b; };
D. Anonymous functions cannot access variables or parameters

Solution

  1. Step 1: Understand variable access in anonymous functions

    Anonymous functions can access outside variables by use or accept parameters.
  2. Step 2: Analyze options

    $sumFunc = function() use ($a, $b) { echo $a + $b; }; uses use ($a, $b) to import variables. $sumFunc = function($a, $b) { echo $a + $b; }; $sumFunc($a, $b); passes variables as parameters. Both are valid.
  3. Final Answer:

    Both A and B are correct ways -> Option A
  4. Quick Check:

    Use 'use' or parameters to access variables inside anonymous functions [OK]
Hint: Use 'use' or function parameters to access variables inside [OK]
Common Mistakes:
  • Forgetting to import variables or pass as parameters
  • Assuming anonymous functions cannot take parameters
  • Trying to access outside variables without 'use'
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