October 29, 2026

PHP – Functions

In this tutorial, we would talk about the important concept of functions in PHP. Function in PHP work similar to other languages like Python and Perl. A function is a block of code that performs some actions and returns a result.

Previously, we discussed two inbuilt functions: fopen() an fread().  Now we would see how to create our own functions. You will learn the following:

  1. Creating a Function
  2. Function Parameters
  3. Arguments by Reference
  4. PHP Function Return Values
  5. Default Function Parameters
  6. Calling Functions Dynamically

 

1. Creating a Function

To create a function in PHP, you use the function keyword. Then inside curly braces, you write the code to be executed by the function. After creating a function, you can use it by calling it.

Let’s take a simple example. We write a function that displays a simple message out to the browser.

<?php
    /* Defining a Function */
    function showMessage() {
    echo "You are learning about PHP Functions!";
    }
    
    /* Calling the Function */
    showMessage();
?>

This function will execute and display the output on the browser

You are learning about PHP Functions!

 

2. Function Parameters

Parameter allow you to provides some data for the function to work with. They are like inputs to a function. You specify the function parameters in brackets before the body of the functions.

<?php
    /* Function to multiply two numbers */
    function multiply($number1, $number2) {
        $product = $number1 * $number2;
        echo "Product of $number1 and $number2 is $product";
    }
    
    /* Calling the Function */
    multiply(5, 10);
?>

As an exercise, execute the code and tell me what output you have!

 

3. Arguments by Reference

In PHP, you can pass parameters to functions by reference. This means that the argument given to the function is a reference to the variable. Therefore the function manipulates this reference rather than the actual value.

If the function makes any changes to the arguments, the change would reflect on the original value of the variable. To pass an argument by reference, you simply add an ampersand(&) to the variable name.

<?php
    /* Function to multiply two numbers */
    function addTen(&$number) {
        $number += 10;
    }

    function addTwenty(&$number) {
        $number += 20;
    }
    
    /* Calling the Function */
    $originalValue = 10;
    addTen($originalValue);
    
    echo "Original Value: $originalValue<br />";
    
    addTwenty( $originalValue );
    echo "Original Value: $originalValue<br />";
?>

The output of the program is:

Original Value: 20
Original Value: 40

What happens here is that the original value is changed by each of the functions. By the first function call, the values changes from 10 to 20 by the addTen() function. The second function addTwenty() changes the value again from 20 to 40. So you see that the calls are made using reference to the original value.

 

4. PHP Function Return Values

A function could also return some value as result of its operation. This is specified using the return statement. When the function encounters a return statement, it stops execution and sends the value back to the calling code. Also, more than one value can be returned using a return array().

The code below, takes two numbers, multiplies them and returns the result back to the calling program.

<?php
    //Returning value
    function multiply($val1, $val2) {
        $product = $val1 * $val2;
        return $product;
    }

    $result = multiply(15, 3);
    echo "The return value is: $result";
?>

 

5. Default Function Parameters

Sometimes you may need to specify a default value for a function parameter. So in case the calling function does not pass any parameter but the function needs a parameter, then the default values are used.

The function below outputs 0 case a parameter was not passed.

<?php
    //Funtion's default value
    function showNum($default = 0) {
        echo "Total input is $default <br>";
    }

    showNum(10);
    showNum();
?>

The output is:

Total input is 10
Total input is 0

In the first line the function uses the passed in parameter of 10 while in the second line, it uses the default parameter of 0.

 

6. Calling Functions Dynamically

You can call a function dynamically. This means that you can assign a function name as string to variables. Then that variable can then be used in place of the function name. An example is given below:

<?php
    //Assigning functions to variables
    function welcome($name) {
        echo "Hey $name, you are welcome";
    }

    $functionVar = "welcome";
    $functionVar("Kindson");
?>

This also works fine. Try it and see the output