Functions help you organize your code by grouping instructions together. You can run the same set of instructions many times without rewriting them.
Function declaration and calling in PHP
Introduction
Syntax
PHP
<?php function functionName() { // code to run } functionName(); // calling the function ?>
Functions start with the keyword function followed by the name and parentheses.
To run the function, you write its name followed by parentheses.
Examples
PHP
<?php function sayHello() { echo "Hello!\n"; } sayHello();
PHP
<?php function greet() { echo "Good morning!\n"; } greet();
Sample Program
This program defines a function that adds two numbers and prints the result. Then it calls the function to run it.
PHP
<?php function addNumbers() { $sum = 5 + 3; echo "Sum is: $sum\n"; } addNumbers();
Important Notes
Function names should be clear and describe what the function does.
Functions can be called many times after they are declared.
Summary
Functions group code to do a specific job.
Declare a function with function keyword and call it by its name.
Using functions makes your code cleaner and easier to manage.
Practice
1. What is the correct way to declare a function named
greet in PHP?easy
Solution
Step 1: Recognize PHP function declaration syntax
In PHP, functions are declared using the keywordfunctionfollowed by the function name and parentheses.Step 2: Match the correct syntax
function greet() { } usesfunction greet() { }, which is the correct PHP syntax for declaring a function namedgreet.Final Answer:
function greet() { } -> Option AQuick 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
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(); usessayHello();, which is the correct way to call the function namedsayHello.Final Answer:
sayHello(); -> Option BQuick 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?
function add($a, $b) {
return $a + $b;
}
echo add(3, 4);medium
Solution
Step 1: Understand the function behavior
The functionaddtakes two parameters and returns their sum.Step 2: Calculate the return value
Callingadd(3, 4)returns 3 + 4 = 7, which is then printed byecho.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
Hint: Functions return values; echo prints the result [OK]
Common Mistakes:
- Concatenating numbers as strings instead of adding
- Expecting function name to print instead of return value
- Syntax errors from missing semicolons
4. Identify the error in the following PHP code:
function greet() {
echo "Hello!";
}
greet;medium
Solution
Step 1: Check function call syntax
The functiongreetis declared correctly, but when calling it, parentheses are missing.Step 2: Understand PHP function call requirements
In PHP, functions must be called with parentheses, even if they take no arguments. Sogreet;is invalid; it should begreet();.Final Answer:
Missing parentheses when calling the function -> Option CQuick Check:
Function calls need () even if no arguments [OK]
Hint: Always use () to call functions, even if empty [OK]
Common Mistakes:
- Calling functions without parentheses
- Confusing function declaration with call
- Missing semicolons elsewhere
5. Consider the following PHP code:
What will be the output and why?
function multiply($x, $y = 2) {
return $x * $y;
}
echo multiply(5);What will be the output and why?
hard
Solution
Step 1: Understand default parameter usage
The functionmultiplyhas two parameters, where$yhas a default value of 2.Step 2: Analyze the function call
Callingmultiply(5)passes 5 as$x, and since$yis not provided, it uses the default 2.Step 3: Calculate the result
The function returns 5 * 2 = 10, which is printed byecho.Final Answer:
10 because the second parameter uses default value 2 -> Option DQuick Check:
5 * 2 = 10 with default parameter [OK]
Hint: Default parameters fill missing arguments automatically [OK]
Common Mistakes:
- Assuming all parameters must be passed
- Ignoring default values
- Expecting error when missing arguments
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
