Writing Functions In PHP – Flexibility

In our last example, we created say_hello() function which is something that has the ability to take a variable or an argument passed into it ($word), and we passed a string to it. If you missed the first tutorial, I recommend you read through that before starting this one.

Passing A Variable Into A Function.

Our previous example, we managed to pass a simple string into a function like so:

<html>
<head>
<title>Writing Functions In PHP - Flexibility</title>
</head>
<body>
<?php
    function say_hello($word){
        echo &quot;Hello {$word}!&quot;;
    }
    say_hello(&quot;Everyone&quot;); /* passing a string value into the function */
?>
</body>
</html>

It should make perfect sense to you that we can also pass a variable into a function. We do that by defining a variable and assigning a value to it. Lets take our previous function for this example.

<html>
<head>
<title>Writing Functions In PHP - Flexibility</title>
</head>
<body>
<?php
    function say_hello($word){
        echo &quot;Hello {$word}!<br>&quot;;
    }
    say_hello(&quot;Everyone&quot;); /* passing a string value into the function */

    $name = &quot;Monie&quot;; /* declare a variable and assign a value to it */
    say_hello($name); /* passing a variable into the function */
?>
</body>
</html>

What we are dong here is passing in a variable which is just a reference to the string into the function. So this will do what we’ve expected, it will reuse our say_hello() function and echo out “Hello Monie!”

Flexible Function With More Arguments

We’ve talked about arguments in our last tutorial, the benefit of having them is that it makes our function more flexible. We can actually make our functions even more flexible if we need to. Let me describe with an example.

<html>
<head>
<title>Writing Functions In PHP - Flexibility</title>
</head>
<body>
<?php
/* declaring our first function */
    function say_hello($word){
        echo &quot;Hello {$word}!<br>&quot;;
    }
    say_hello(&quot;Everyone&quot;); /* passing a string value into the function */

    $name = &quot;Monie&quot;; /* declare a variable and assign a value to it */
    say_hello($name); /* passing a variable into the function */

/* declaring our second function */
    function say_hello2($greeting, $person, $punct){
        echo $greeting .&quot;, &quot;. $person . $punct . &quot;<br>&quot;;
    }
    say_hello2(&quot;Greetings&quot;, $name, &quot;!&quot;); /* calling out the function */
?>
</body>
</html>

Output:
Hello Everyone!
Hello Monie!
Greetings, Monie!

As you can see, we have three different arguments defined to the function, “$greeting”, “$person” and “$punct”. When we call the function, we actually need to pass three arguments to the function as well. This is the most important feature of a function. We always need to call them with the exact same number of arguments that we’ve defined them with. Any less than that will return an error.

So we’ve made our function even more flexible by using more arguments. It’s up to you to determine what needs to be flexible and what doesn’t. After all, having all this complex customized function isn’t necessarily a good thing for you. That might be a real pain for you. It might be a lot better to let your function do some of the work and know how to make an assumption. It is what we call an Error Resistant Function. Sure, it would be a more complex flexible function but fortunately I will teach you how later on in this tutorial series.

Function With Returning Values

Most of the time, we will need this kind of function when we need to get a value or a result out of a function so that we can keep using it and keep working with it other that just displaying them with an echo statement. Consider this example:

We are trying to count how much money a person get for his overtime work. Each month, a person may or may not be working overtime. That’s why the total hours of overtime is set to a variable and not included in the function. The calculation will be done outside of the function or perhaps be sent to another function as an argument or variable. That means we need to get the value out of the function, which is the value of the person’s hourly pay salary, and them multiply it (another function) with the total number of overtime hours the person has.

Salary: $2000
Days of work in a month: 24 days
Total hours of work in a day: 8 hours
Total hours of overtime: Variable
Overtime rate: 1.5

To get yourself clear with our example in calculating the person’s overtime, here is the formula that I’ll be using.

(($salary / $month / $day) x $rate) x Total hours of overtime

<html>
<head>
<title>Writing Functions In PHP - Flexibility</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>

In the above function, there is now a way for us to manipulate the value from inside the function and then use it outside of the function. It just displays the $hourly_pay value to us but we need to multiply it with the total hours of overtime to get the exact figure of what we are trying to archieve here.

So how can we return or send the value out of the function so that we can use them once the function has finished execution?  Join me on the next article to learn more about function with it’s capabilities for returning values.

Got something to say? Go for it!