0
0
PHPprogramming~10 mins

Anonymous function syntax in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an anonymous function assigned to $greet.

PHP
$greet = [1]() { echo "Hello!"; };
Drag options to blanks, or click blank then click option'
Afun
Bdef
Clambda
Dfunction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'def' or 'lambda' which are not PHP keywords.
Forgetting the 'function' keyword.
2fill in blank
medium

Complete the code to call the anonymous function stored in $greet.

PHP
$greet[1]();
Drag options to blanks, or click blank then click option'
A->
B()
C[]
D->call
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->' which is for object properties.
Using '[]' which is for arrays.
3fill in blank
hard

Fix the error in the anonymous function syntax.

PHP
$sum = function($a, $b) [1] return $a + $b; };
Drag options to blanks, or click blank then click option'
A{
B(
C;
D[
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or brackets instead of curly braces.
Missing the opening brace causes syntax errors.
4fill in blank
hard

Fill the blank to create an anonymous function that uses a variable from outside its scope.

PHP
$multiplier = 3;
$multiply = function($number) use [1] { return $number * $multiplier; };
Drag options to blanks, or click blank then click option'
A{$multiplier}
B[$multiplier]
C($multiplier)
D<$multiplier>
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or curly braces after 'use' instead of parentheses.
Forgetting the opening curly brace for the function body.
5fill in blank
hard

Fill the two blanks to create and call an anonymous function that returns the square of a number.

PHP
$square = function($n) [1] return $n * $n; [2]
echo $square(5);
Drag options to blanks, or click blank then click option'
A()
B{
C}
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of parentheses for parameters.
Missing opening or closing curly braces.