PHP - Functions
A function is a block of statements which executes only when it is called somewhere in the program. A function provides re-usability of same code for different inputs, hence saves time and resources. PHP has many built-in functions and one of the common function is echo() which is used to print the output. Users can also create their own function which is also termed as user-defined functions.
Create Function
In PHP, creating a function starts with keyword function followed by function's name and parenthesis containing function's parameter(s), if it has any. At last, it contains block of statements which is also called body of the function.
Call Function
After defining the function, it can be called anywhere in the program with it's name followed by parenthesis containing function's parameter(s), if it has any and semicolon ;.
Syntax
//Defining function function function_name(parameters) { statements; } //Calling function function_name(parameters);
Example: A function with no parameter
In the example below, a function HelloWorld is created to print Hello World!. It requires no parameters to execute.
<?php function HelloWorld() { echo "Hello World!"; } HelloWorld(); ?>
The output of the above code will be:
Hello World!
Parameter
A parameter (or also known as argument) is a variable which is used to pass information inside a function. In above example, the function does not has any parameter. But a user can create a function with single or multiple parameters. Value of a parameter can be further used by the function to achieve desired result.
Example: A function with parameters
In the example below, the function called MyFunction is created which requires two integer numbers as parameters and print sum of two numbers in desired style.
<?php function MyFunction($x, $y){ $z = $x + $y; echo "Sum of $x and $y is: $z.\n"; } $a = 15; $b = 10; MyFunction($a, $b); ?>
The output of the above code will be:
Sum of 15 and 10 is: 25
Function to Return Values
A function can also be used to return values. In the example below, the function called MyFunction returns sum of two digits.
<?php function MyFunction($x, $y){ $z = $x + $y; return $z; } $a = 15; $b = 10; echo MyFunction($a, $b); ?>
The output of the above code will be:
25
Default Parameter Value
Default value can be assigned to a parameter at the time of creating function. When the function is called without parameter then it uses the default value.
Example:
In the example below, the MyFunction function is used to add two, three and four integer numbers. The function uses the default value of a given parameter when the function is called without it. The default value of a parameter is set while defining the function.
<?php function MyFunction($p, $q, $r=0, $s=0){ return $p+$q+$r+$s; } $a = 15; $b = 10; $c = 5; $d = 1; echo MyFunction($a,$b)."\n"; echo MyFunction($a,$b,$c)."\n"; echo MyFunction($a,$b,$c,$d)."\n"; ?>
The output of the above code will be:
25 30 31
Data Type declaration of Parameters of a function
In PHP, there is no need to declare data type of parameters of a function. PHP automatically takes the data type of a variable, depending on its value. This can lead to an error when string is passed through a function instead of numeric data type. Therefore, in PHP 7 and onwards, type declaration is added. This gives an option to specify data type of parameters.
Example:
In the example below, a function called MyFunction is defined which takes integer data type as arguments.
<?php function MyFunction(int $x, int $y){ $z = $x + $y; return $z; } $a = 15; $b = 10; echo MyFunction($a, $b); ?>
The output of the above code will be:
25
By adding the strict declaration, it throws "Fatal Error" in case of data type mismatch. This can be done by putting first line of PHP script as declare(strict_types=1);.
<?php declare(strict_types=1); function MyFunction(int $x, int $y){ $z = $x + $y; return $z; } $a = 15.5; $b = 10; echo MyFunction($a, $b); ?>
The output of the above code will be:
PHP Fatal error: Uncaught TypeError
Return Type declaration of a function
PHP 7 and onwards supports return data type declaration. It can be achieved by adding a colon : followed by type declaration.
Example:
In the example below, a function called MyFunction is defined. It is declared to return float data type.
<?php declare(strict_types=1); function MyFunction(float $x, int $y) : float { $z = $x * $y; return $z; } $a = 15.25; $b = 10; echo MyFunction($a, $b); ?>
The output of the above code will be:
152.5
Dynamic function calls
In PHP, it is possible to assign function names as strings to a variable and then treat that variable exactly as new function name itself.
Example:
In the example below, a function called MyPrint is defined. The name of this function is assigned to a variable called MyFunction. The function is then called by this variable.
<?php function MyPrint($name) { echo "Hello $name"; } $MyFunction = "MyPrint"; $MyFunction("John"); ?>
The output of the above code will be:
Hello John