PHP Functions

PHP function is a piece of code that can be reused many times in program. In php there are different type of functions, we are using these functions depending upon their need and uses.


Advantage of using functions :

  1. Code Reusability : PHP functions are defined only once and that can be used anywhere in programs
  2.  Easy To Understand : PHP functions support the modular design and that enables user to separate the programming logic depending upon their need. Also this helps to understand the programming flow of application better as it works on modular design.
  3.  Less Code : we don't have to write the same code or same logic many times. Reusability functionality makes the programming block short, simple and precise.
  4.  Save Time : it is saving the time as user only reusing the same functional block rather writing the same code again.

Steps to remember while  designing functions :
  1. A function name can start with a letter or underscore (not a number).
  2. Give the function a name that reflects what the function does.
  3. The opening curly brace ( { ) indicates the beginning of the function code and the closing curly brace ( } ) indicates the end of the function.
  4. Function names are case-insensitive.

       
        <?php
            function displayMessage() {
                echo "Welcome to php function example";
            }

            displayMessage(); // call the function
        ?>