Functions

☰ Menu Content


A function is name given to a block of code that can be executed whenever we need it. 

Function freed us from writing the same code repeatedly.

For example, you have a company slogan that you want to display on every webpage. 

Creating Your First PHP Function

Function needs a name, lets name our first function  CompanySlogan

We use this "name" to call the function, so the name should be easy to understand.

First, you must tell PHP that you want to create a function. 

Do this by typing the keyword "function" followed by the function "name" and some other stuff.

Here is how you would make a function called CompanySlogan

PHP Code:
    <?php

            function CompanySlogan()
            {

            }

    ?>

Note: Function name can start with a letter or underscore "_", but not a number!

Now fill in the code that we want our function to execute. 

Do you see the curly braces in the above example "{}"?

These braces define where our function's code goes. 

The opening curly brace "{" tells php that the function's code is starting 

Closing curly brace "}" tells PHP that our function is done!

We want our function to print out the company motto each time it's called.

PHP Code:
    <?php

        function CompanySlogan()
        {
            echo "We deliver first class material!<br />";
        }

    ?>

Using Your PHP Function

Now that we completed coding a PHP function, let us test it.

Below is a simple PHP script, let us add to it the above "CompanySlogan" function twice.

PHP Code:
<?php
    echo "Welcome to Myanmaraphp.com <br />";
    echo "Well, thanks for stopping by! <br />";
    echo "and remember... <br />";
?>
PHP Code with Function:
<?php

    function CompanySlogan()
    {
        echo "We deliver first class material!<br />";
    }

    echo "Welcome to MyanmarPhp.com <br />";
    CompanySlogan();
    echo "Well, thanks for stopping by! <br />";
    echo "and remember... <br />";
    CompanySlogan();

?>
Display:
Welcome to MyanmarPhp.com
We deliver first class material!
Well, thanks for stopping by!
and remember...
We deliver first class material!

When you are creating a function, follow these simple guidelines:-

  • Always start your function with the keyword function
  • Remember that your function's code must be between the "{" and the "}"
  • When you are using your function, be sure you spell the function name correctly
PHP Functions - Parameters

"CompanySlogan" function above only print out a single unchanging string..

Extra information can be supplied to a  function so as to make them more useful.

Information is supplied to a function by using "parameters"

Let's create a new function to see how "parameter" is used.

PHP Code with Function:
<?php

    function Greeting($firstName)
    {
        echo "Hello there ". $firstName . "!<br />";
    }

?>

When we use our Greeting function we must supply it a string containing someone's name.

If we don't do this the code will break.

Let's call our new function a few times with some common first names.

PHP Code:
<?php

    function Greeting($firstName)
    {
        echo "Hello there ". $firstName . "!<br />";
    }

    Greeting("Joe");
    Greeting("Anne");
    Greeting("Jill");
    Greeting("Charles");

?>
Display:
Hello there Joe!
Hello there Anne!
Hello there Jill!
Hello there Charles!

Multiple parameters can be supplied to a function. 

Just separate multiple parameters PHP uses a comma ",". 

Let's modify our function to also include last names.

PHP Code:
<?php

    function Greeting($firstName, $lastName)
    {
        echo "Hello there ". $firstName ." ". $lastName ."!<br />";
    }

    Greeting("Joe", "Blue");
    Greeting("Anne", "Zerro");
    Greeting("Jill", "Roberto");
    Greeting("Charles", "Smith");

?>
Display:
Hello there Jack Blue!
Hello there Ahmed Zerro!
Hello there Julie Roberto!
Hello there Charles Smith!

PHP Functions - Returning Values

Besides being able to pass information to a functions, you can make them return a value. 

But a function can only return one thing, the return can be any integer, float, array, string, etc --

When a function is made to return a value it will execute the code first before sending back the return value.

To capture this value you can set a variable equal to the function. Something like:

$myVar = somefunction();

The example below returns the sum of two integers.

PHP Code:
<?php

    function Sum($numX, $numY)
    {
        $total = $numX + $numY;
        return $total;
    }

    $Number = 0;

    echo "Before the function, Number = ". $Number ."<br />";
    // Store the result of Sum in $Number
    $Number = Sum(3, 4);
    echo "After the function, Number = " . $Number ."<br />";

?>
Display:
Before the function, Number = 0
After the function, Number = 7

We first print out the value of $Number that is still set to the original value of 0. 

When we set $Number equal to the function Sum, $umber is set equal to Sum's result. 

In this case, the result was 3 + 4 = 7, which was stored in $Number.

$Number displayed in the second echo statement!

Note For more detail please see Php Manual at http://www.php.net

Php Tutorial Content (menu)