How to use Function-return value in PHP

In this article, I will explain how the Function-return value can be used in PHP.
  • 2718

Function-return value in PHP

  • A function can return a value using the return statement in conjunction with a value or object.

  • In return value function, return keyword is used to return a value from a function.

  • In return value function, return stops the execution of the function and sends the value back to the calling code.

Syntax

 function functionname(parameters)

  {

  code to be executed;

  return value

  }

Example

The following example takes two integer parameters and add them together and then returns their total.

<html>

<head>

<title>Function which returns value</title>

</head>

<body>

<h2 style="color: steelblue;">Function with return value example</h2>

    <?php

    function add($num1, $num2)

    {

    $total = $num1 + $num2;

    return $total;

    }

    $return_value = add(50, 70);

    echo "Returned value from the function : $return_value";

    ?>

</body>

</html>

 

Output

f-return.jpg

You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
© 2019 DotNetHeaven. All rights reserved.