PHP Tutorial – Functions

The power of the PHP language is the large number of built-in functions (more than 700 built-in function and counting.) But of course it also possible to create your own functions. In this PHP tutorial we will create our own functions.

A function will only be executed if a function is called by another piece of code. You can see that this is very handy. For example you can keep the browser from executing a script when the page loads by putting your script into a function. Only when the function is called (for example by pushing a button) the function is executed.

Syntax


function functionName()
{
      code to be executed;
}

Some guidelines when creating a PHP function:

  • The function name should reflect what the function does.
  • The function name can start with an underscore or letter. A number is not allowed.

A Function Example

So let’s look at a function example:


<html>
<body>

<?php
    function ourFunction()
    {
         echo "black";
    }

    echo "My car has the color ";
    ourFunction();
?>

</body>
</html>

The result of this example will be:


My car has the color black

Also look at the next tutorial where we will explain function parameters in PHP programming language.

That is all for this PHP programming tutorials.

This entry was posted in PHP Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.