A PHP function is a block of statements that can be reused many times. It can take input as an argument list and return value.
PHP function will be only executed by calling the function.
PHP provides various built-in functions to the user. User can also create user-defined functions too.
There are two types of function.
PHP provides various built-in functions. Every PHP built-in function performs some specific task. Let us see it with the help of an example.
PHP user-defined function starts with the function
keyword followed by function name and arguments.
function functionName(arg1, arg2, ....) {
//block of code to be executed
}
A function can not run until it is called. Please keep in mind that a function is called by its name followed by ()
.
A user-defined function can call as follows:
functionName(); // calling function
Source Code
<?php
function greetMsg() {
echo "I have understood php function.";
}
greetMsg(); // call the function
?>
PHP function containing single argument:
Source Code
<?php
function fruits($name) {
echo 'I like to eat ' . $name ;
echo '<br>'; // line break
}
fruits('mango');
fruits('orange');
fruits('banana');
?>
PHP function containing multiple arguments:
Source Code
<?php
function fruits($name, $city, $cost) {
echo "$name is selling inside $city at the rate of $cost";
}
fruits('mango', 'salempur', 200);
fruits('orange', 'salempur', 50);
?>
There are two ways to pass arguments to a function. An argument can be passed to a function using call by value and by reference. By default function argument is passed by value.
Passing argument to a function by reference, allows us to modify function arguments. That is the main reason behind the passing argument by reference.
Passing an argument by reference can be done by prepending ampersand (&) to the argument name in the function definition.
Let us understand the above mention concept using an example.
Source Code
<?php
function fruits($name) {
$name .= ' is one of the best fruit';
echo $name . '<br>';
// outputs "mango is one of the best fruit"
}
$fruitName = 'mango';
fruits($fruitName);
echo 'Outside the function: ' . $fruitName; // it is stil 'mango'
?>
Code Explanation:
Therefore, it is clear that a variable value can be changed inside the function when it passes inside the function by reference. Just prepend an & sign to the argument name in the function definition.
Source Code
<?php
function fruits(&$name) {
$name .= ' is one of the best fruit';
echo $name . '<br>'; // outputs "mango is one of the best fruit"
}
$fruitName = 'mango';
fruits($fruitName);
echo 'Outside the function: ' . $fruitName; // it is stil 'mango'
?>
The default value for a function's argument can be provided by using assignment operator (=
) in the function definition. If the calling function, does not provide the value then the function will be used as the default value of the argument.
Let us understand with the help of an example.
Source Code
<?php
function fruits($=fruitName = 'mango') {
echo "The number is: $number <br>";
}
fruits('orange');
fruits('grapes');
fruits();
// will print ,mango, the default value
?>
A function return statement is used to return the value to the calling function. Whenever PHP parser parses the return statement inside the function then it returns the function's value to the calling function and stops the execution of the function.
Source Code
<?php
function addNumber($x, $y) {
$sum = $x + $y;
return $sum;
}
$returnedValue = addNumber(20, 70);
echo "Function returned the summation of two number:
$returnedValue";
?>
It is possible to assign function names as strings to variables and then treat these variables exactly as the function name itself.
Source Code
<?php
function fruit() {
echo "Mango is one of the best fruit";
}
$functionName = 'fruit';
$functionName(); // called fruit function
?>