• Post category:PHP

How to Return Value From Function in PHP

PHP Return Value From Function

When PHP encounters a return statement inside a function, it halts the processing of the function and “returns” control to the main body of the program. Let’s see the example of return value from function in PHP

Ex:

<?php
function cube($n){
return $n*$n*$n;
}

echo "Cube of 3 is : " . cube(3);
?>

Output:
Cube of 3 is: 27

Factorial program return value

<?php 
  function factorial($x)
  {
   for($f=1, $i=1; $i<=$x; $i++)
      $f *= $i;
    return ($f);
  }
echo "</br> Factorial of 5 = " . factorial(5);
echo "</br> Factorial of 7 = " . factorial(7);
?>

Output:

php return value from function

Leave a Reply