FUNCTION IN PHP
![](https://blog.codehunger.in/wp-content/uploads/2022/08/function-in-php_2075.jpg)
In php their are thousands of in-bulit function.But user can also defined the function according to requirements
PHP User-defined Functions
In php user can defined the function by using function keyword .
Syntax for creating the user defined function
function function_Name() {
//write your code...
}
Example
<?PHP
function print() {
echo "Your name is Rahul kumar";
}
print(); //call the function
?>
In above example we create a function name print() by using function keyword.We write one statement that will print the message over the screen.But to see the message on the screen.You required to call the function to see the output.
PHP supports their is call by value and call by reference function .so let’s study about these.
PHP Call By Value
In PHP call by value function like as parameterized function .like if you accept some value from a form then you want to perform the operation like add ,subtraction ,division etc..so you pass that parameter into the function.In call by value means that when you call the function you pass some value in the function .
So let’s take an example for better understanding..
<?PHP
$a=10;
$b=15;
function add($first,$second) {
return $first+$second;
}
$result = add($a,$b);
echo "ADDITION OF ".$a." and ".$b." is ".$result;
?>
In above code we take two values stored in $a ,$b variable .we did pass these variable during calling time to function that is called call by value .instead of passing value we pass the variable which stores the actual value.
CALL BY REFERENCE
In this method, the address of actual parameters is passing to the function. So any change made by function affects actual parameter value.
let’s see the example of call by reference
<?php
$a = 10;
echo "The actual value before function call :".$a."<br>";
function change(&$parameter) {
$parameter = $parameter + 2;
return $parameter;
}
$result = change($a);
echo "The Actual value of $a is :".$a."<br>";
echo "The function return value :".$result."<br>";
?>
In above example when we write the function we write parameter with & this .when we are calling the function so this function actually takes the address of the variable and function when return the value so it change the actual value of $a.
In call by reference and call by value their is a difference. that call by reference change the value of actual variable. but call by value doesn’t change the value of actual variable .