PHP Functions are blocks of reusable code that are designed to perform a specific task. Understand its Classifications, Arguments & default values with simple examples:
In this tutorial, you will learn what a PHP function is, built-in functions (such as PHP empty(), count(), etc.), user-defined functions, parameters, arguments & default values, returning values, recursive & anonymous PHP functions, and frequently asked questions (FAQs).
Please note that we have used PHP version 7 in all examples.
Let’s begin!
Table of Contents:
PHP Functions: Guide with Examples

What is a PHP Function
A PHP function is a piece of reusable code. It may take input as an argument (or parameter) and return a value after some processing. You may have already seen some functions in our previous tutorials, as they are widely used.
The following piece of code shows the syntax of a function.
<?php
function funcName(){
//executable code
}
?>
A function name must start with a letter or an underscore symbol, and it cannot start with a number or any other symbol.
Let’s consider the following example. What would you expect after running this code?
<?php
function saySomething(){
echo "Hello World!";
}
?>
Hello World! as output?
No, nothing will display. To get an output onto your browser (screen), you need to call the function. Otherwise, even if the function is working properly, it will not produce any output to the user.
Now, see the modified example below. We need to call the function just outside the function, and not inside it.
You can practice this example by running the following programming code:
<?php
function saySomething(){
echo "Hello World!";
}
saySomething(); //calling the function
?>
Output:
Hello World!
Some of the advantages of using functions are less code needed, code reusability, easy-to-understand and maintain code, and easy error detection.
Classification of Functions
There are two major types of PHP functions. They are:
- Built-in functions
- User-defined functions
Built-in Functions
There are over 1000 built-in functions in PHP. Due to the space limit, we have only shown some frequently used ones in the list below.
- array_key_exists()
- array_keys()
- array_map()
- array_merge()
- class_exists()
- count()
- date()
- dirname()
- empty()
- explode()
- file_exists()
- file_get_contents()
- get_class()
- implode()
- in_array()
- is_array()
- is_null()
- is_object()
- is_string()
- json_encode()
- preg_match()
- preg_replace()
- sprintf()
- str_replace()
- strlen()
- strpos()
- strtolower()
- substr()
- time()
- trim()
Let’s see a couple of examples. You can practice these examples by running the following programming code.
Example 1: count() – It counts the total number of elements in an array.
<?php
$fruits = array('apple','mango','banana');
echo count($fruits);
?>
Output:
3
Example 2: date() – It formats a local time/date.
<?php
echo date('l jS \of F Y');
?>
Output:
Wednesday 21st of July 2021
Note: It will display the current date in the given format. Also, note that you can customize this format.
User-defined Functions
The user (developer or programmer) can create their functions. They are called user-defined functions.
Let’s see an example. You can practice this example by running the following programming code:
<?php
function displayName(){
echo "Hi, Jane Doe!";
}
displayName(); //calling the function
?>
Output:
Hi, Jane Doe!
Parameters, Arguments, and Default Values
Even though parameters and arguments look similar, they are not the same.
A parameter is a variable defined by a function. It gets a value when the function is called. A function can take one parameter or several parameters separated by a comma.
On the other hand, an argument is a value that is passed to a function.
Let’s see some examples. You can practice these examples by running the following programming code:
Example 1:
<?php
function myFunc1($x,$y) {
$z=$x + $y;
echo $z."<br/>";
}
myFunc1(10,20);
?>
Output:
30
In the above example, parameters are $x and $y. Arguments are 10 and 20.
Example 2:
<?php
function myFunc2($x=100,$y=200) {
$z=$x + $y;
echo $z;
}
myFunc2();
?>
Output:
300
Variables can be arguments. In the above example, myFunc2 takes two arguments, $x and $y (not 100 and 200).
Example 3:
<?php
function sayHi($name){
echo "Hi $name<br/>";
}
sayHi("Alex");
sayHi("David");
sayHi("John");
?>
Output:
Hi Alex Hi David Hi John
Example 4:
<?php
function getInfo($name,$age){
echo "$name is $age years old.<br/>";
}
getInfo("Alex", 18);
getInfo("David", 21);
getInfo("John", 19);
?>
Output:
Alex is 18 years old. David is 21 years old. John is 19 years old.
Example 5:
<?php
function getInfo($name,$age=19){
echo "$name is $age years old.<br/>";
}
getInfo("Alex", 18);
getInfo("David", 21);
getInfo("John");
?>
Output:
Alex is 18 years old. David is 21 years old. John is 19 years old.
In this example, we didn’t specifically pass the age of John when calling the function. Therefore, it has taken the value of age as 19. In other words, the value 19 acts as the default value for $age.
Passing Arguments by Reference
Usually, arguments are passed by value. In addition, we can pass the arguments by reference too. The & operator is used to indicate arguments passed by reference.
Let’s see an example. You can practice this example by running the following programming code:
<?php
function addition(&$val) {
$val += 20;
}
$num = 5;
addition($num);
echo $num;
?>
Output:
25
Returning Values
Functions can return values. It uses the return keyword to return values.
Let’s see an example. You can practice this example by running the following programming code:
<?php
function mySum($num1, $num2, $num3){
$sum = $num1 + $num2 + $num3;
return $sum."<br/>";
}
echo mySum(1, 2, 3); // sum of 1+2+3 which is 6
echo mySum(10, 10, 20); // sum of 10+10+20 which is 40
?>
Output:
6 40
PHP Recursive Functions
Functions that call themselves are known as recursive functions.
Let’s see an example. You can practice this example by running the following programming code:
<?php
function printNum($num) {
if($num<=5){
echo "$num <br/>";
printNum($num+1);
}
}
printNum(1);
?>
Output:
1 2 3 4 5
Note: For a better understanding of the above example, you need to know about PHP if statements.
PHP Anonymous Functions
Anonymous functions are functions that have no names.
Let’s see an example. You can practice this example by running the following programming code:
<?php
$var = function() {
echo "I Am Anonymous.";
};
$var();
?>
Output:
I Am Anonymous.
Frequently Asked Questions
1. What is a PHP function?
It is a code segment that takes input as an argument. After some processing, PHP functions return values. Furthermore, functions can be reused several times.
2. What are the two main types of functions?
They are built-in and user-defined functions.
3. What is a user-defined function in PHP?
It is a function created on your own.
<?php
function myFunc(){
echo “Hello world!”;
}
myFunc ();
?>
4. Why do we use functions?
We use them for different purposes, such as:
Use as an alternative to having repeated code blocks. Hence, less code is required.
• For the reusability of code.
• For a better understanding of the application flow.
• To detect errors easily.
• To maintain code easily.
5. Can a function call itself in PHP?
Yes, it is called a recursive function.
6. What is PHP empty()?
PHP empty() is a built-in function that checks whether a variable is empty or not. This PHP function returns true if the variable is empty. Otherwise, it returns false.
Conclusion
A function is a block of code that can be reused.
It can take input as arguments and return values. There are over 1000 built-in functions available in PHP, and you can also build your functions.






