Writing Functions In PHP – Return Values

Writing Functions In PHP – Return Values

By Jeevan M Paran on February 15th, 2010 in Tutorials

In the last two tutorials in this series, we've been looking at functions. In this tutorial, we are going to look specifically into when a function can return a value back out of them.

If you are new to this series please check out the other previous tutorials:

“Writing Functions In PHP – Introduction”.

“Writing Functions In PHP – Flexibility”.

Referring back to our previous overtime() function:

<html>
<head>
<title>Writing Functions In PHP - Return Values</title>
</head>
<body>
<?php
    /* declaring our third function */
    function overtime($salary, $month, $day, $rate){
        $hourly_pay = ($salary / $month / $day) * $rate;
        echo $hourly_pay;
    }
    overtime(2000,24,8,1.5); /* passing four arguments into the function */
?>
</body>
</html>

Output: 15.625

Function With Return Values

We know that we must get the value out of the function (instead of just displaying it in order for us to keep using it and keep working with it even if the function has finished execution. In order to do that, we need to return the value of $hourly_pay from the function and that’s just done with simply the word return, that’s the function that we are calling. We are telling the function, return this value!

It also is going to exit out of the function at that point when we say return. It’s a lot like break was when we were working with the loop. Let’s update our overtime() function so that it can return a value out of the function. The only thing that you should do is to replace the word “echo” with “return” and some modification in how we call the function.

<html>
<head>
<title>Writing Functions In PHP - Return Values</title>
</head>
<body>
<?php
    /* declaring our third function */
    function overtime($salary, $month, $day, $rate){
        $hourly_pay = ($salary / $month / $day) * $rate;
        return $hourly_pay;
    }
    $returned_value = overtime(2000,24,8, 1.5);
    echo $returned_value;
?>
</body>
</html>

So we called the function overtime(), we are going to return the value $hourly_pay to $returned_value and then echo back the new value.

Now, its a good idea whenever we are working with functions, especially functions that don’t just do display like we did with say_hello(), that you  always return a value out of them. Just get in a habit of making sure every function has a return. Maybe all it returns is true or false based on whether it works successfully, but we want to have something returned out of the end of that function just to let you know.

So for our say_hello() function, we should write something like this:

<?php
   function say_hello(){
     echo &quot;Hello World!&quot;;
     return true;
   }
   say_hello();
?>

So, a quick tip for you. You could actually have several return values inside a function. You could have it like this:

<?php
   function multiple_return($val2, $val2){
      if this happens{
         return this value;
      }
      if that happens{
         return this value;
      }
   }
?>

You could even have a function that had dozens of return value in it and a final return value at the very end of the function so that if none of the others were true, non of the others happen, than it would return something like FALSE or a DEFAULT value, which is a topic that I’ll be talking about in the final tutorial series.

Of course if I am returning a value, then I also have to receive (or catch) the value at the other end. Unlike say_hello() function where I called it on it’s own,

<?php
   function say_hello($word){
     echo &quot;Hello {$word}!&quot;;
   }
   say_hello(&quot;Everyone&quot;);
?>

I set it to the variable $returned_value so I can echo it back.

<?php
    $returned_value = overtime(2000,24,8, 1.5);
    echo $returned_value;
?>

The next return value tip that I want to tell you about is that return values return one and only one value. If you need to return more than one value, there is a way to do that.

Function With More Than One Return Values

For this, let me explain in a new example.

<html>
<head>
<title>Writing Functions In PHP - Return Values</title>
</head>
<body>
<?php
    function add_subt($val1, $val2){
        $add = $val1 + $val2;
        $subt = $val1 - $val2;
        return $add;
    }
?>
</body>
</html>

This function will take the two values I give it, it will do both addition and subtraction, and right now it’s going to return addition. Subtraction will just get lost like nothing ever happen. We can’t return more than one so we have to choose. But what if we want both?

Array is going to be our friendly solution here. Let’s see how array can help us.

<html>
<head>
<title>Writing Functions In PHP - Return Values</title>
</head>
<body>
<?php
    function add_subt($val1, $val2){
        $add = $val1 + $val2;
        $subt = $val1 - $val2;
        $result = array($add, $subt);
        return $result;
    }
    $result_array = add_subt(10,5);
    echo &quot;Add: &quot; . $result_array[0] . &quot;<br>&quot;;
    echo &quot;Subt: &quot; . $result_array[1] . &quot;<br>&quot;;
?>
</body>
</html>

Output: Add: 15
Output: Subt: 5

Let me guide you in the solution provided.

$result = array($add, $subt);

The reason we use array is because array simply takes variables and puts them into a single structure and assigns it to a single value like the above example. So when we want to echo both of the returned values, we call them like so:

    $result_array = add_subt(10,5);
    echo &quot;Add: &quot; . $result_array[0] . &quot;<br>&quot;;
    echo &quot;Subt: &quot; . $result_array[1] . &quot;<br>&quot;;

The number in the bracket [0] and [1] indicates the $add and the $subt variable from the function attributes that we called earlier: array($add, $subt); So [0] for additional and [1] for subtraction.

So that’s the way that we can pass multiple results out of the function. So if we need to pass something more that just one value, we can do it with an array.

Now that we’ve talked about return values. We’ve seen how a functions can do it’s processing and return a result back to what ever called the function. Next, we will look into Global Variables, a variable that is accessible from any method, procedure, or function and whose value can therefore be changed anywhere in the application.

Did you like it? Share it!
  • Delicious
  • DesignBump
  • DesignFloat
  • Digg
  • Facebook
  • StumbleUpon
  • Technorati
  • Twitter
About The Author
Jeevan M Paran

Jeevan M Paran @ Monie, a freelance web designer and he extremely enjoy and actively learning web design programming languages from simple HTML, wonderful ASP to amazing PHP. You can see his updates on his blog portfolio at Monieweb.com

Comments
  • Dave
    February 15th, 2010 at 11:31 am.

    Thanks. Please keep the tuts coming for php learners and the inexperienced (like me!) I learned a lot!

  • February 17th, 2010 at 3:35 am.

    You can read some extra knowledge from TIZAG or W3SCHOOLS.

Trackbacks

Toggle Trackbacks

  1. [... If you are new to this series please check out the other previous tutorials: "Writing Functions In PHP – Introduction". "Writing Functions In PHP – Flexibility". Referring back to our previous overtime() function: [php] Writing ...]

So, what do you think?

x