PHP functions

Discussion in 'Web Design & Programming' started by RHochstenbach, Mar 10, 2011.

  1. RHochstenbach

    RHochstenbach Administrator Staff Member

    Likes Received:
    26
    Trophy Points:
    48
    Let's say you want to make a page that converts US Dollars to Euros. 1 US Dollar is worth 0.723 Euros, so you have to multiple the amount of Dollars by 0.723. If you make a list of 1-5 Dollars and their value when converted to Euros inside a table, you can do something like this:

    HTML:
    <table>
    <tr>
    <th>Dollar</th>
    <th>Euro</th>
    </tr>
    <tr>
    <td>1</td>
    <td>
    <?php
    $euro = 1 * 0.723;
    echo $euro;
    ?>
    </td>
    </tr>
    <tr>
    <td>2</td>
    <td>
    <?php
    $euro = 2 * 0.723;
    echo $euro;
    ?>
    </td>
    </tr>
    <tr>
    <td>3</td>
    <td>
    <?php
    $euro = 3 * 0.723;
    echo $euro;
    ?>
    </td>
    </tr>
    <tr>
    <td>4</td>
    <td>
    <?php
    $euro = 4 * 0.723;
    echo $euro;
    ?>
    </td>
    </tr>
    <tr>
    <td>5</td>
    <td>
    <?php
    $euro = 5 * 0.723;
    echo $euro;
    ?>
    </td>
    </tr>
    </table>
    
    
    Notice the length of the code? And that's with just 5 entries! We can make this smaller using a feature of PHP, called functions.

    Basically a function accepts a value, does something with it, and then returns a result.

    We need to define a function before we can use it.

    PHP:
    function name_of_function() {

    }
    As you can see, we use the instruction 'function' to define a function, followed by a unique name, two parentheses and two brackets. Inside the parentesis you should put a non-existing variable. Like this:
    PHP:
    function name_of_function($variable) {

    }
    To use a function in the page, you need to call it. You call a function like this:
    PHP:
    name_of_function(10);
    I defined 10 in this example. Now $variable gets set to 10. We can now do something special here. We change the function so that it will multiple the variable with 10.

    PHP:
    function myfunction($number) {
    return 
    $number 10;
    }
    Do you see the return instruction I've put in the function? This means that this value replaces the function call in your document. In this case it multiples the input which was assigned to the variable by 10. If you would place this directly in a PHP script, you would have to use echo to print it out. Therefore you should call the function like this:
    PHP:
    echo myfunction(10);
    Now load the page in your web browser. It now shows '100'.

    To summarize, when you call a function this happens:

    1. The value between parentheses get's assigned to the variable which has been put between parentheses in our function. So $number gets set to 10.
    2. The function performs all instructions that have been set. We haven't set any in our example.
    3. The return instruction now replaces our function by the line that has been set there. So in this case it does $number * 10.

    Note that functions aren't limited to an echo statement. For example I can have my function multiply the input by 10 and later on add 1 to it, like this:

    PHP:
    function myfunction($number) {
    return 
    $number 10;
    }

    // The function multiplies the number by 10 and assigns the result in the $value variable.
    $value myfunction(10);

    // Adds 1 to the $value variable and shows the result
    echo $value 1;
    With this information in mind, it's now time to re-write our first code using a function:
    Code:
    <?php
    function dollar_to_euro($amount) {
    
    $value = $amount * 0.723;
    // the return statement contains a table row, the amount we give to the variable and the converted amount.
    return "<tr><td>".$amount ."</td><td>".$value ."</td></tr>";
    }
    ?>
    <table>
    <tr>
    <th>Dollar</th>
    <th>Euro</th>
    </tr>
    
    <!-- If you put a number between parenthesis, it will build a table row.
    It contains the amount of dollars in the left column and the converted amount
    in Euros in the right column.
    
    Looks cleaner, doesn't it? -->
    
    <?php echo dollar_to_euro(1); ?> <!-- 1 dollar -->
    
    <?php echo dollar_to_euro(2); ?><!-- 2 dollars -->
    
    <?php echo dollar_to_euro(3); ?><!-- 3 dollars -->
    
    <?php echo dollar_to_euro(4); ?>
    
    <?php echo dollar_to_euro(5); ?>
    
    </table>
    
    
    We can even generate a loop which shows amounts in groups of 10 all to way up t0 1000 dollars:
    PHP:
    function dollar_to_euro($amount) {
    $value $amount 0.723;
    return 
    "<tr><td>".$amount ."</td><td>".$value ."</td></tr>";
    }

    echo 
    "<table>";
    echo 
    "<tr><th>Dollar</th><th>Euro</th></tr>";
    for ( 
    $counter 10$counter <= 1000$counter += 10) {
    echo 
    dollar_to_euro($counter);
    }
    echo 
    "</table>";
    Although it would require the same amount of code when you just put this inside the loop and don't use the function, but image very large amounts of repeating code. Instead of copying and pasting the same code all over again, you could just create functions and call them :)
     

Share This Page