Bird
Raised Fist0
PHPprogramming~20 mins

Why closures matter in PHP - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Closure Master in PHP
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a closure capturing a variable
What is the output of this PHP code that uses a closure capturing a variable by reference?
PHP
<?php
$counter = 0;
$increment = function() use (&$counter) {
    $counter++;
    return $counter;
};

echo $increment();
echo $increment();
?>
A11
B01
C00
D12
Attempts:
2 left
💡 Hint
Closures can capture variables by reference, so changes inside affect the original variable.
🧠 Conceptual
intermediate
1:30remaining
Why use closures in PHP?
Which of the following best explains why closures matter in PHP?
AThey prevent any variable from being modified inside the function.
BThey allow functions to access variables from the parent scope even after that scope has ended.
CThey automatically convert all variables to global scope.
DThey replace the need for classes and objects entirely.
Attempts:
2 left
💡 Hint
Think about how closures keep variables alive beyond their normal lifetime.
🔧 Debug
advanced
2:30remaining
Identify the error with closure variable capture
What error will this PHP code produce and why?
PHP
<?php
$funcs = [];
for ($i = 0; $i < 3; $i++) {
    $funcs[] = function() {
        return $i;
    };
}
foreach ($funcs as $f) {
    echo $f();
}
?>
AOutputs '333' because $i is not captured correctly
BSyntax error due to missing use keyword
COutputs '012' as expected
DNotice: Undefined variable $i
Attempts:
2 left
💡 Hint
Variables inside closures must be explicitly captured with use().
📝 Syntax
advanced
1:30remaining
Correct syntax for closure with parameters
Which option shows the correct syntax for a PHP closure that takes one parameter and returns its square?
A$square = function($x) { return $x * $x; };
B$square = function $x { return $x * $x; };
C$square = function($x) => $x * $x;
D$square = function { return $x * $x; }($x);
Attempts:
2 left
💡 Hint
Closures use the function keyword followed by parentheses for parameters.
🚀 Application
expert
3:00remaining
Using closures to create a counter generator
Which option correctly creates a closure in PHP that returns a function acting as a counter starting from 0 and increasing by 1 each call?
A
&lt;?php
function counter() {
    $count = 0;
    return function() {
        return ++$count;
    };
}
$c = counter();
echo $c();
echo $c();
?&gt;
B
&lt;?php
function counter() {
    static $count = 0;
    return function() {
        return ++$count;
    };
}
$c = counter();
echo $c();
echo $c();
?&gt;
C
&lt;?php
function counter() {
    $count = 0;
    return function() use (&amp;$count) {
        return ++$count;
    };
}
$c = counter();
echo $c();
echo $c();
?&gt;
D
&lt;?php
function counter() {
    $count = 0;
    return function() use ($count) {
        return ++$count;
    };
}
$c = counter();
echo $c();
echo $c();
?&gt;
Attempts:
2 left
💡 Hint
To keep the count variable updated across calls, capture it by reference.

Practice

(1/5)
1. What is a closure in PHP?
easy
A. A named function that cannot access outside variables
B. An anonymous function that can access variables from its parent scope
C. A class method that is always static
D. A variable that stores a string value

Solution

  1. Step 1: Understand the definition of closure and compare options

    A closure is an anonymous function that can remember and use variables from the scope where it was created. Only An anonymous function that can access variables from its parent scope describes an anonymous function with access to outer variables, which matches the closure concept.
  2. Final Answer:

    An anonymous function that can access variables from its parent scope -> Option B
  3. Quick Check:

    Closure = Anonymous function with outer scope access [OK]
Hint: Closures are anonymous functions with memory of outside variables [OK]
Common Mistakes:
  • Confusing closures with regular named functions
  • Thinking closures cannot access outside variables
  • Mixing closures with class methods
2. Which of the following is the correct syntax to create a closure in PHP?
easy
A. $func = closure() { echo 'Hello'; };
B. function $func() { echo 'Hello'; };
C. $func = function() { echo 'Hello'; };
D. $func = new function() { echo 'Hello'; };

Solution

  1. Step 1: Recall PHP closure syntax and check options

    Closures are created using the keyword function without a name, assigned to a variable. $func = function() { echo 'Hello'; }; uses function() { ... } assigned to $func, which is correct syntax.
  2. Final Answer:

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

    Closure syntax = function() { ... } [OK]
Hint: Closures use 'function() { }' assigned to variables [OK]
Common Mistakes:
  • Using 'closure()' keyword which does not exist
  • Trying to name the anonymous function
  • Using 'new function()' which is invalid
3. What will be the output of this PHP code?
$message = 'Hi';
$greet = function() use ($message) {
echo $message;
};
$greet();
medium
A. HiHi
B. Error: Undefined variable
C. No output
D. Hi

Solution

  1. Step 1: Understand the use keyword and trace execution

    The closure uses use ($message) to import the variable $message from the outer scope. When $greet() is called, it echoes the value of $message, which is 'Hi'.
  2. Final Answer:

    Hi -> Option D
  3. Quick Check:

    Closure with use outputs outer variable value [OK]
Hint: Closures use 'use' to access outside variables [OK]
Common Mistakes:
  • Forgetting to use 'use' keyword to import variables
  • Expecting closure to access outer variables automatically
  • Confusing output with repeated prints
4. Identify the error in this PHP closure code:
$count = 0;
$increment = function() {
$count++;
};
$increment();
echo $count;
medium
A. Closure cannot access $count without 'use' keyword
B. Syntax error: missing semicolon after function
C. Cannot increment variables inside closures
D. No error, outputs 1

Solution

  1. Step 1: Analyze variable access and PHP scope rules in closures

    The closure tries to increment $count but does not import it with use, so $count is undefined inside. Variables outside closures must be imported with use to be accessible inside closures.
  2. Final Answer:

    Closure cannot access $count without 'use' keyword -> Option A
  3. Quick Check:

    Use 'use' to access outer variables inside closures [OK]
Hint: Always import outer variables with 'use' in closures [OK]
Common Mistakes:
  • Assuming closures access outer variables automatically
  • Thinking increment inside closure changes outer variable without 'use'
  • Confusing syntax errors with scope errors
5. How can closures help in creating private data in PHP? Choose the best explanation.
hard
A. Closures can capture variables in their scope, hiding them from outside access
B. Closures automatically make variables global
C. Closures convert variables to constants
D. Closures prevent any function from accessing variables

Solution

  1. Step 1: Understand private data and how closures enable it

    Private data means variables that cannot be accessed directly from outside code. Closures can enclose variables in their scope, so only the closure can access or modify them, hiding them from outside.
  2. Final Answer:

    Closures can capture variables in their scope, hiding them from outside access -> Option A
  3. Quick Check:

    Closures enable private data by capturing variables [OK]
Hint: Closures hide variables by capturing them inside functions [OK]
Common Mistakes:
  • Thinking closures make variables global
  • Confusing closures with constants
  • Believing closures block all variable access
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