functions in PHP and types of function in PHP

In web development, It’s considered best practice to split code into small functions that can be reused in further applications per requirement. Before starting let’s understand more about functions.

There are more than a thousand in-built functions provided by PHP like string functions, array functions, and more.

What is Function?

A function is a block of statements that can be used repeatedly in a program. It is executed only when called. It can have arguments and return values as per requirements.

We can create functions with 4 types in PHP:

  • Function without arguments and return value
  • Function with arguments
  • Function with return values
  • Function with arguments and return values

Syntax :

PHP allows user to define their function. Below is a syntax for defining function :

function functionName($arguments){
    //code
    return values; //Optional
}

Function declaration starts with the function keyword. Another thing is the function name which needs to be unique to the program and it can’t be a reserved keyword or an in-built function name like split or in_array. Arguments and return types are optional. Let’s take examples for all types of functions.

Function Without Arguments and Return Value

Here, we define a function that just prints a message whenever it’s called.

<?php
    // defining function
    function welcome() {
        echo "Welcome to Codewolfy!";
    }

    welcome();   // calling function
?>

In the above example, we have defined the welcome function and called it. This will simply print a welcome message.

Functions With Arguments

PHP function can have arguments or parameters. Those parameters play a role in the function’s code. Let’s take an example where the user passes two parameters and the function will print the addition of those parameters.

<?php
    function sum($a, $b) {
        echo $a + $b;
    }

    sum(10,20);
    sum(50,70);
?>

In this example, we have defined the sum() function. Which will take 2 arguments and print sum of those parameters. Here, we have called the sum function two times so it will print 2 values.

We can also set a default value for those parameters. In case, while calling function user doesn’t pass arguments then it will use the default value and execute the code. Have a look at the below example for the default argument value :

<?php
    function sum($a = 10, $b = 20) {
        echo $a + $b;
    }

    sum();
    sum(50,70);
?>

Using the sum function the first time will take 10 and 20 as the default value while calling the second time will take the parameter’s value to perform addition.

Functions With Return Value

The concept of the return value is used when we need to get an output of the function into the main function or code where we called the function. i.e. we want to define a function to get some values like a welcome message.

<?php
    function welcome() {
        return "Welcome to Codewolfy!";
    }

    echo welcome();
?>

Here, we have defined the same function as the first example but this function will return a welcome message in a stand of printing it. so we can use that message in further code.

Functions With Arguments and Return Value

This type is a combination of function with argument and function with return value. Which means we can pass parameters and return values too. Let’s take an example of it.

<?php
    function sum($a, $b) {
        return $a + $b;
    }

    echo sum(10, 20);
?>

Same as the previous sum function it will take two values and perform addition but in a stand of printing it will return a new value to its initiator and we can use that value in further operations. This function type is generally used in actual programming. Here, we just perform logic based on parameters and return output.

Conclusion

In this article, we have learned about functions in PHP and its types with examples. In web development, functions are very useful for reusing code and saving time. So try to use functions as much as you can.