What is a function?
A function is a named block of code that performs a specific task.
PHP provides us with two major types of functions:
- Built-in functions : PHP provides us with huge collection of built-in library functions. These functions are already coded and stored in form of functions. To use those we just need to call them as per our requirement like, var_dump, fopen(), print_r(), gettype() and so on.
- User Defined Functions : Apart from the built-in functions, PHP allows us to create our own customised functions called the user-defined functions.
Using this we can create our own packages of code and use it wherever necessary by simply calling it.
Define a function
While creating a user defined function we need to keep few things in mind:
- Any name ending with an open and closed parenthesis is a function.
- A function name always begins with the keyword function.
- To call a function we just need to write its name followed by the parenthesis.
- A function name cannot start with a number. It can start with an alphabet or underscore.
- A function name is not case-sensitive.
Syntax:
<?php
function function_name() {
statement;
}
- First, specify the function name followed by the function keyword. The name of the function needs to start with a letter or underscore followed by zero or more letters, underscores, and digits.
- Second, define one or more statements inside the function body. The function body starts with the { and ends with }.
Ex:
1 2 3 4 5 6 | <?php function welcome() { echo 'Welcome'; } |
Call a function
When a function doesn’t have any parameter, you can call the function by using its name followed by parentheses like this:
<?php
function_name();
Ex:
1 2 3 4 5 6 7 8 | <?php function welcome() { echo 'Welcome!'; } welcome(); //Welcome! |
And when you call the function with parameters, you need to pass arguments into it:
Ex:
1 2 3 4 5 6 7 | <?php function welcome_user($username) { echo 'Welcome ' . $username; } welcome_user('Admin'); // Welcome Admin! |
Function Parameters
A function can have zero or more parameters:
<?php
function function_name (parameter_list)
{
}
When a function has multiple parameters, you need to separate them using a comma (,).
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php function concat($str1, $str2) { return $str1 . $str2; } $greeting = concat( 'Welcome ', 'Home' ); echo $greeting; |
Passing arguments by values
Consider the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $counter = 1; function increase($value) { $value+= 1; echo $value. <br>; // 2 } // increase the counter increase($counter); echo $counter . <br>; // 1 |
- First, define the $counter variable and initialize its value to one.
- Second, define the increase() function that increases the argument by one and displays it.
- Third, call the increase() function and pass the $counter variable into the function.
- Finally, display the $counter variable.
Passing arguments by reference
To pass an argument by reference, you prepend the operator (&) to the parameter name in the function definition like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $counter = 1; function increase( &$value ) { $value += 1; echo $value . <br>; // 2 } // increase the counter increase($counter); echo $counter . <br>; // 2 |
In this example, the change of the $counter variable reflects both inside and outside the function.
Default Parameters
The following defines the concat() function that concatenates strings :
1 2 3 4 5 6 7 8 9 10 | <?php function concat($str1, $str2, $delimiter = ' ') { return $str1 . $delimiter . $str2; } $message = concat('Hi', 'there!', ','); echo $message; |
Output:
1 | Hi,there! |
In this example, we passed a comma to the $delimiter. The concat() function used the comma (,) instead of the default argument.
The order of default parameters
When you use default parameters, it’s a good practice to place them after the parameters that don’t have default values. Otherwise, you will get unexpected behavior.
Ex:
1 2 3 4 5 6 7 8 9 10 | <?php function concat($delimiter = ' ', $str1, $str2) { return $str1 . $delimiter . $str2; } $message = concat('Hi', 'there!', ','); echo $message; |
Output:
1 | there!Hi, |
Variadic Functions
The following example defines a function called sum() that returns the sum of two integers:
1 2 3 4 5 6 7 8 | <?php function sum(int $x, int $y) { return $x + $y; } echo sum(10, 20); // 30 |
To allow the sum() function to accept a variable of arguments, you need to use func_get_args() function. The func_num_args() function returns an array that contains all function arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php function sum() { $numbers = func_get_args(); $total = 0; for ($i = 0; $i < count($numbers); $i++) { $total += $numbers[$i]; } return $total; } echo sum(10, 20) . '<br>'; // 30 echo sum(10, 20, 30) . '<br>'; // 60 |
PHP 5.6 introduced the … operator. When you place the … operator in front of a function parameter, the function will accept a variable number of arguments, and the parameter will become an array inside the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php function sum(...$numbers) { $total = 0; for ($i = 0; $i < count($numbers); $i++) { $total += $numbers[$i]; } return $total; } echo sum(10, 20) . '<br>'; // 30 echo sum(10, 20, 30) . '<br>'; // 60 |
PHP 7 allows you to declare types for variadic arguments.
1 2 3 4 5 6 7 8 9 10 11 | <?php function sum(int ...$numbers): int { $total = 0; for ($i = 0; $i < count($numbers); $i++) { $total += $numbers[$i]; } return $total; } |