PHP functions are the same as other programming languages. A function is a piece of code that takes a single input in the form of a parameter and performs a certain processing and returns value.

You have already seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you the option to create your own functions too.

There are two parts to consider -

  • Creating a PHP functions
  • Calling PHP functions

In fact you never need to create your own PHP functions because there are already over 1000 built-in library functions just you need to call.


Creating PHP Function

It's very easy to create your own PHP function. Suppose you want to create a PHP function that will simply write a simple message in your browser where you will call it. Following an example creates a function called writeMessage() and calls it as soon as you build it.

Note that when constructing a function the name must start with the keyword function and all PHP code must be enclosed in {and} brackets as shown in the following example below -

#example_function.php

<?php
         /* Defining a PHP Function */
         function writeMessage() {
            echo "Love, Success, Inspirational and Motivational etc";
         }
         
         /* Calling a PHP Function */
         writeMessage();
 ?>

output:

Love, Success, Inspirational and Motivational etc

PHP Functions with Parameters

PHP gives you the option to transfer your parameters within the function. You can skip as many parameters as you like. These frameworks serve as a variation within your work. Following the example takes two complete parameters and puts them together and prints them.

 <?php
         function addFunction($num1, $num2) {
            $sum = $num1 + $num2;
            echo "Sum of the two numbers is : $sum";
         }
         
         addFunction(10, 20);
      ?>

output:

Sum of the two numbers is : 30

Passing Arguments by Reference

It is possible to pass arguments to functions by reference. This means that the variable reference is controlled by the function rather than a copy of the variable value.

<?php
         function addFive($num) {
            $num += 5;
         }
         
         function addSix(&$num) {
            $num += 6;
         }
         
         $orignum = 10;
         addFive( $orignum );
         
         echo "Original Value is $orignum";
         
         addSix( $orignum );
         echo "Original Value is $orignum";
 ?>

output:

Original Value is 10
Original Value is 16 

Setting Default Values for Function Parameters

You can set the parameter to have a default value if the caller does not.

Following function prints NULL in case the use does not exceed the value of the function.

<?php
         function printMe($param = NULL) {
            print $param;
         }

         printMe("This is test");
         printMe();
  ?>


Output:

This is test

Dynamic Function Calls

It is possible to assign function names as strings to the variable and manage these variables as you will create the actual function. Following the example illustrates this behavior.

 <?php
         function sayHello() {
            echo "Hello
"
; } $function_holder = "sayHello"; $function_holder(); ?>

output:

Hello