Saturday, 7 February 2015

Function Return Values

In previous examples we just print the value from functions ,but if we need to do other tasks like need some results of calculations or some statements ,for this type of scenario in php we use return values in function Example:

<?php
// define a function for calculation
function calculate($val1,$val2,$opr) { 
   
if($opr=='add'){
return $val1+$val2;  // return value 
}
if($opr=='sub'){
return $val1-$val2;  // return value 
}
if($opr=='mul'){
return $val1*$val2;  // return value 
}
    

//call a function where first two parameters values and other mention operation you need to perform

$result = calculate(2,5,'mul');  //multiply operation

echo $result; 

//print output 10
?>


No comments: