A function is a reusable block of code that performs a specific task. You can call it whenever you need that task done.
Click to reveal answer
beginner
How do you declare a function in PHP?
Use the <code>function</code> keyword, followed by the function name and parentheses. Inside curly braces, write the code to run when the function is called.<br><br>Example:<br><code>function greet() { echo 'Hello!'; }</code>
Click to reveal answer
beginner
How do you call a function in PHP?
Write the function name followed by parentheses. If the function needs inputs, put them inside the parentheses.
Example: greet();
Click to reveal answer
beginner
What happens if you call a function that has parameters?
You must provide the required values (arguments) inside the parentheses when calling the function. These values are used inside the function.<br><br>Example:<br><code>function sayHello($name) { echo "Hello, $name!"; }<br>sayHello('Alice');</code>
Click to reveal answer
beginner
Can a function return a value in PHP? How?
Yes, a function can send back a value using the <code>return</code> keyword. This lets you use the result elsewhere.<br><br>Example:<br><code>function add($a, $b) { return $a + $b; }<br>$sum = add(3, 4);</code>
Click to reveal answer
Which keyword is used to declare a function in PHP?
Afunction
Bdef
Cfunc
Ddeclare
✗ Incorrect
In PHP, the keyword function is used to declare a function.
How do you call a function named greet in PHP?
Acall greet();
Brun greet();
Cfunction greet();
Dgreet();
✗ Incorrect
To call a function, just write its name followed by parentheses: greet();
What will this code output?
function sayHi() { echo 'Hi!'; } sayHi();
AError
BsayHi
CHi!
DNothing
✗ Incorrect
The function sayHi prints 'Hi!' when called.
What is needed when calling a function that requires parameters?
ANothing, just call it
BProvide the required arguments inside parentheses
CDeclare the function again
DUse return keyword
✗ Incorrect
You must provide the required arguments inside parentheses when calling a function with parameters.
What does the return keyword do in a function?
ASends a value back to where the function was called
BPrints a message
CStops the program
DDeclares a variable
✗ Incorrect
The return keyword sends a value back to the place where the function was called.
Explain how to declare and call a function in PHP with an example.
Think about the basic structure and how you use the function after declaring it.
You got /5 concepts.
Describe how parameters and return values work in PHP functions.
Consider how data goes into and comes out of a function.
You got /4 concepts.
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?