2. PHP User-defined functions
PHP has a large number of built-in functions such as mathematical, string, date, array functions etc. It is also
possible to define a function as per specific requirement. Such function is called user defined function.
A function is a reusable block of statements that performs a specific task. This block is defined with function
keyword and is given a name that starts with an alphabet or underscore. This function may be called from
anywhere within the program any number of times.
Syntax
3. Function may be defined with optional but any number of arguments.
However, same number of arguments must be provided while calling.
Function's body can contain any valid PHP code i.e. conditionals,
loops etc. (even other functions or classes may be defined inside a
function). After executing statements in the block, program control
goes back to the location from which it was invoked irrespective of
presence of last statement of function block as return. An expression
in front of return statement returns its value to calling environment.
4. user defined function Example
<?php
//function definition
function sayHello()
{
echo "Hello World!";
}
//function call
sayHello();
?>
Output
Hello World!
5. function with arguments
<?php
function add($arg1, $arg2){
echo $arg1+$arg2 . "n";
}
add(10,20);
add("Hello", "World");
?>
Example
Output
30
PHP Warning: A non-numeric value encountered in line 3
In second call, two string values are given as function arguments. Since PHP doesn't support + operator for
strings, a warning is emitted.
6. function return
User defined function in following example processes the provided arguments and returns a value to calling
environment
Example
<?php
function add($arg1, $arg2){
return $arg1+$arg2;
}
$val=add(10,20);
echo "addition:". $val. "n";
$val=add("10","20");
echo "addition: $val";
?>
Output
addition:30
addition:30
In second call, even if arguments are string, PHP coerces them into integer and performs addition
7. function with default argument value
While defining a function , a default value of argument may be assigned. If value is not assigned to such argument
while calling the function, this default will be used for processing inside function. In following example, a function is
defined with argument having default value
Example
<?php
function welcome($user="Guest"){
echo "Hello $usern";
}
//overrides default
welcome("admin");
//uses default
welcome();
?>
Output
Hello admin
Hello Guest
In second call, function is called without passing value. In this case, user argument takes its default value.
8. Function with variable number of arguments
It is possible to define a function with ability to receive variable number of arguments. The name of formal
argument in function definition is prefixed by ... token. Following example has add() function that adds a list of
numbers given as argument
Example
<?php
function add(...$numbers){
$ttl=0;
foreach ($numbers as $num){
$ttl=$ttl+$num;
}
return $ttl;
}
$total=add(10,15,20);
echo "total= $totaln";
echo "total=". add(1,2,3,4,5). "n";
?>
Output
total= 45
total=15
9. <?php
function add(){
$numbers=func_get_args();
$ttl=0;
foreach ($numbers as $num){
$ttl=$ttl+$num;
}
return $ttl;
}
$total=add(10,15,20);
echo "total= $totaln";
echo "total=". add(1,2,3,4,5). "n";
?>
Output
total= 45
total=15
It is also possible to obtain a list of arguments passed to a function with the help of func_get_args() function. We
can run a PHP loop to traverse each value in the list of arguments passed. In that case the function definition
doesn't have a formal argument.
10. Function within another function
A function may be defined inside another function's body block. However, inner function can not be called before
outer function has been invoked.
Example
<?php
function hello(){
echo "Hellon";
function welcome(){
echo "Welcome to the world of programmingn";
}
}
//welcome();
hello();
welcome();
?>
Remove the comment to call wlcome() bfore hello(). Following error message halts the program −
Fatal error: Uncaught Error: Call to undefined function welcome()
Output
Hello
Welcome to the world of programming
11. Recursive function
A function that calls itself is called recursive function. Calling itself unconditionally creates infinite loop and results
in out of memory error because of stack full. Following program calls factorial() function recursively
Example
<?php
function factorial($n){
if ($n < 2) {
return 1;
} else {
return ($n * factorial($n-1));
}
}
echo "factorial(5) = ". factorial(5);
?>
Output
factorial(5) = 120