📖 Scenario: You are creating a simple PHP program to greet users by their name. This is like writing a small helper that says hello whenever you ask it.
🎯 Goal: Build a PHP program that declares a function called greet which takes a name and returns a greeting message. Then call this function with a specific name and print the greeting.
📋 What You'll Learn
Declare a function named greet that accepts one parameter called name.
The function should return a string greeting in the format: "Hello, {name}!".
Call the greet function with the argument "Alice".
Print the result of the function call.
💡 Why This Matters
🌍 Real World
Functions help organize code into reusable blocks, like helpers that do one job well. This is common in all programming tasks.
💼 Career
Knowing how to declare and call functions is essential for any PHP developer to write clean, maintainable code.
Progress0 / 4 steps
1
Declare the greet function
Write a PHP function declaration named greet that takes one parameter called name. Inside the function, return the string "Hello, " concatenated with the name parameter and an exclamation mark "!".
PHP
Hint
Use the function keyword, then the function name greet, and parentheses with $name inside. Use return to send back the greeting string.
2
Call the greet function with "Alice"
Create a variable called message and set it to the result of calling the greet function with the argument "Alice".
PHP
Hint
Assign the result of greet("Alice") to the variable $message.
3
Print the greeting message
Use echo to print the variable message.
PHP
Hint
Use echo $message; to display the greeting on the screen.
4
Run the program and see the output
Run the PHP script and observe the output. It should print Hello, Alice! exactly.
PHP
Hint
Make sure you run the PHP file in a server or command line to see the output.
Practice
(1/5)
1. What is the correct way to declare a function named greet in PHP?
easy
A. function greet() { }
B. def greet() { }
C. func greet() { }
D. function: greet() { }
Solution
Step 1: Recognize PHP function declaration syntax
In PHP, functions are declared using the keyword function followed by the function name and parentheses.
Step 2: Match the correct syntax
function greet() { } uses function greet() { }, which is the correct PHP syntax for declaring a function named greet.
Final Answer:
function greet() { } -> Option A
Quick Check:
PHP function declaration = function greet() { } [OK]
Hint: Use 'function' keyword to declare functions in PHP [OK]
Common Mistakes:
Using 'def' instead of 'function'
Adding a colon after 'function'
Using 'func' keyword which is not valid in PHP
2. Which of the following is the correct way to call a function named sayHello in PHP?
easy
A. call sayHello();
B. sayHello();
C. sayHello;
D. callFunction(sayHello);
Solution
Step 1: Understand PHP function call syntax
In PHP, to call a function, you write the function name followed by parentheses, optionally with arguments inside.
Step 2: Identify the correct option
sayHello(); uses sayHello();, which is the correct way to call the function named sayHello.
Final Answer:
sayHello(); -> Option B
Quick Check:
Function call syntax = sayHello(); [OK]
Hint: Call functions by name with parentheses () [OK]
Common Mistakes:
Using 'call' keyword which does not exist in PHP
Omitting parentheses when calling functions
Using incorrect function call syntax like callFunction()
3. What will be the output of the following PHP code?