What is a Function?
Function is a independent set of code statements, written to perform some tasks.
Function may or may not accept / return a value.
Function names are case-insensitive in PHP.
In PHP there are mainly 2 types of Function.
1) built-in (ready to use)
2) user-defined (created by user)
Why to use a Function?
Functions are written mainly because of reusability.(Write once and use forever)
Functions are easy to manage, simple to read.
How to use a Function?
function uniquename (arguments)
return value/variable; // optional return statement
The uniquename can be any string that starts with a letter or underscore.
Function Arguments / Parameters:
Depending on type, Parameters to a function can be passed in 2 ways.
Call by Value:
copy of variables are passed as parameters. hence original value is not changed.
function addthem( $n1, $n2)
$sum = $n1 + $n2;
echo " Addition = " .addthem(123,321);
Call by Reference:
Address of variables are passed as parameters, hence original value may be changed.
echo "Before swap : a=".$a ." and b = $b<br/>";
echo "After swap : a=".$a ." and b = $b<br/>";