📖 Scenario: You are creating a small PHP script to greet users with a custom message. You want to use an anonymous function to keep your code neat and reusable.
🎯 Goal: Build a PHP script that defines an anonymous function to greet a user by name and then calls that function.
📋 What You'll Learn
Create an anonymous function assigned to a variable called greet
The anonymous function should accept one parameter called name
The function should return a greeting string like "Hello, [name]!"
Call the anonymous function with the name "Alice" and print the result
💡 Why This Matters
🌍 Real World
Anonymous functions are useful for quick, small tasks like callbacks, event handlers, or short reusable code blocks without naming them.
💼 Career
Understanding anonymous functions helps you write cleaner, more flexible PHP code, which is valuable in web development and backend programming jobs.
Progress0 / 4 steps
1
Create the anonymous function variable
Create an anonymous function and assign it to a variable called greet. The function should accept one parameter called name.
PHP
Hint
Use function($name) { } syntax and assign it to $greet.
2
Add greeting message inside the function
Inside the anonymous function assigned to $greet, return the string "Hello, " concatenated with the $name parameter and an exclamation mark.
PHP
Hint
Use the dot operator . to join strings in PHP.
3
Call the anonymous function with a name
Call the anonymous function stored in $greet with the argument "Alice" and assign the result to a variable called $message.
PHP
Hint
Call the function like $greet("Alice") and save it to $message.
4
Print the greeting message
Print the variable $message to display the greeting.
PHP
Hint
Use print($message); to show the greeting.
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
Step 1: Understand function naming
Anonymous functions do not have a name, unlike regular functions.
Step 2: Recognize assignment to variables
They are often assigned to variables for later use or callbacks.
Final Answer:
A function without a name, often assigned to a variable -> Option C
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
Step 1: Identify correct anonymous function syntax
In PHP, anonymous functions use the keyword function without a name and are assigned to variables.