A function is a block of code that takes more than one input in the form of a parameter, processes it, and then returns a value.
There are two types of PHP functions:
- Built-in Functions
- User defined functions
PHP Functions: Built-in
To do a given operation, PHP includes over 1000 built-in functions that may be invoked straight from within a script.
Visit PHP Built-in Functions to know in detail about built-in functions.
PHP Functions: User Defined
In addition to the built-in PHP functions, you can create your own functions, these functions are known as user-defined functions.
There are two parts of php user-defined functions:
- Creating a Function
- Calling a Function
First of all a function is declared and then used by calling it where ever needed.
Creating PHP User-Defined Functions:
The term function appears first in a user-defined function declaration:
Syntax:
function fun-Name() {
code to be executed;
}
Note: The name of a function must begin with a letter or an underscore. Function names are case-insensitive.
Calling PHP User-Defined Functions:
The declared function is called where ever needed. It can be called more then once.
fun-Name();
The above declare function is later on called by its name. Now let’s see an example:
<?php function Msg() { echo "Hello!"; } echo "As Function is called after this echo statement so,The output of particular is shown below this statement </br> </br>"; Msg(); ?>
PHP Function Arguments
An argument is just like a variable we can pass information to functions through arguments.
Syntax:
Arguments are written inside parentheses after the function name. You can use as many arguments as you like; just use a comma to separate them.
function fun-Name($argument1,$argument2)
Let see an example to understand it more clearly:
<?php function Sum($num1,$num2){ echo $num1+$num2; echo "<br />"; } Sum(2,3); //it will show 5 Sum(3,5); //it will show 8 Sum(40,90); //it will show 130 ?>
The above example has a function with arguments ($num1, $num2). When the Sum() function is called, we pass the values against these variables (e.g. 2,3), and these values are used inside the function, which outputs several different results.
PHP Default Argument
You can set default value for an argument so that if the function’s caller doesn’t pass the value then the default value will be used.
<?php function setlength(int $length = 20) { echo "The length is : $length <br>"; } setlength(50); setlength(); setlength(150); setlength(200); ?>
Note: If no value is passed to the function and there is no default value as well, then the function will print NULL.
<?php function setlength(int $length) { echo "The length is : $length <br>"; } setlength(50); setlength(150); setlength(); ?>
Passing Arguments by Reference
Arguments are usually passed by value in PHP, which implies that the function uses a copy of the value and the variable passed into the function cannot be altered. Call by Reference is used to allow a function to alter its arguments.
You must use the ampersand (&) symbol before the parameter name to pass value as a reference.
<?php function add_two(&$value) { $value += 2; } $num = 2; add_two($num); echo "The number is " .$num; ?>
PHP Functions – Return value
Use the return statement to make a function return a value:
<?php function add(int $a, int $b) { $c = $a + $b; return $c; } echo "2 + 11 = " . add(2,11) . "<br>"; echo "1 + 13 = " . add(1,13) . "<br>"; echo "6 + 4 = " . add(6,4); ?>