0
0
PHPprogramming~5 mins

Function declaration and calling in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a function in PHP?
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.<br><br>Example:<br>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
How do you call a function named greet in PHP?
Acall greet();
Brun greet();
Cfunction greet();
Dgreet();
What will this code output?<br>
function sayHi() { echo 'Hi!'; }<br>sayHi();
AError
BsayHi
CHi!
DNothing
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
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
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.