PHP Functions

In this PHP tutorial, you will learn all about conditional statements in PHP. We will discuss in detail the User-defined functions, Parameterized Function, Call by Value, Call by Reference, Default Arguments, Variable Arguments, Return Type Declarations, Recursive Function.

PHP Functions are the same as other programming languages. Functions are the codes that are used to perform certain tasks and return the output. The function gets the input in the form of a function argument. 

What are the advantages of using functions in PHP?

  • Code Reusability: Once a PHP function is defined it is possible to call the function as many times as are required.
  • Easy to understand: PHP functions are used to isolate the programming logic. Because each logic is split into functions, it is easier for people to understand the application's flow.
  • Less Code: It saves a lot of code since we don't have to write the logic over and over again. The usage of functions allows you to write the logic only once and reuse it.

In PHP there are numerous built-in functions for various purposes and similarly, users can also declare the user-defined functions.

What is user-defined in PHP?

Functions declared by the users to fulfill their requirements are known as the user-defined function. A function won’t execute automatically at the time of execution of the program but instead, it will only be executed by calling the function.

How to create a User Defined Function in PHP?

To create a user-defined function we use the keyword function followed by the function name and the round brackets which contain the arguments and curly braces where we write the function code.

Syntax


function functionName() {
  code to be executed;
}

Example :Function in PHP


function welcome () {
    echo "Welcome to learnetutorials.com";
}
welcome ();
 

Output:


Welcome to learnetutorials.com

In the above example we are creating a user-defined function “welcome” to print the string “Welcome to learnetutorials.com”. For that we have created the function using the keyword function followed by the function name “welcome” and we have printed the needed using the echo statement. And outside the function we have called the function “welcome” followed by the round brackets to determine it is function.

What is meant by call by value in PHP?

PHP allows to invoke functions by value as well as by reference. In the case of a PHP call by value, the actual value is not changed but instead the value within the function will be only changed.

Example :call by value in PHP


function countNum ($num) {
    $num += 1; 
}
$num = 1; 
countNum ($num);
echo $num;

 

Output:


1

In the above example we can see that the value we passed as the argument is the variable and the variable created in the parameter of the function will be different from the variable outside the function. Which we have discussed in the variable chapter. As we know that the change made in the block of the function won’t affect the variable outside the function.

What is meant by call by reference in PHP?

In PHP call by reference, the real value is changed even if it is changed within the function. In here the & (ampersand) symbol must be used with arguments. The & symbol denotes the variable's reference.

Example :call by reference in PHP


function countNum(&$num){
    $num += 1; 
}
$num = 1; 
countNum($num);
echo $num;

 

Output:


2

In the above example we can see that the value we passed as the argument is the reference of the variable and the variable created in the parameter of the function will be the reference of the original variable and if we change the value of the variable even inside the function the value of the variable outside the function will also change.

Default argument values function in PHP

In PHP we use the default argument values in case if we don’t pass any argument then the default value assigned in the parameter of the function will be used.

Example :Default argument values function in PHP


function welcome ($name = "Guest") {
    echo "Hai $name, welcome to learnetutorials.com \n";
}
welcome ("John");
welcome ();

 

Output:


Hai John, welcome to learnetutorials.com 
Hai Guest, welcome to learnetutorials.com

In the above example we can see that the value we have assigned the value in the parameter of the function itself. And when we pass the value through the argument at the time of calling the function the value passed will be printed and if we call the function with no argument then the default value will be used.

Variable arguments function in PHP

PHP has a variable-length argument function. This indicates that you can send any number of parameters to the function. To do so, we just have to place three ellipses (dots) before the parameter name.

Example :Variable arguments function in PHP


function welcome (...$names) {
    foreach ($names as $name) {
        echo "Hai $name, welcome to learnetutorials.com \n";
    }
}
welcome ("Jhon", "Roy", "Justin");

 

Output:


Hai Jhon, welcome to learnetutorials.com 
Hai Roy, welcome to learnetutorials.com 
Hai Justin, welcome to learnetutorials.com

In the above example we can see that we have sent three values as the argument and in the parameter of the function instead of declaring three variables we have created a variable argument. And in the block of the function, we have printed the values of the variable argument using the foreach loop.

How to return values in a PHP function?

To return the value in the from the function we use the return statement.

Example :return values in PHP functions


function welcome($name) {
    return "Hai $name, welcome to learnetutorials.com";
}
echo welcome ("Roy");

 

Output:


Hai Roy, welcome to learnetutorials.com

In the above example we can see that we have not printed the string in the function but instead we have returned the value from the string and the return value of the function is printed using the echo statement.

What is meant by return type declarations in PHP?

As PHP is a loosely typed scripting language the return type can be different than one is expected to avoid such a scenario, we are using the return type declaration, by this we can specify the type of the value to be returned from the function. To declare the return type of the function we add a colon (:) before the opening curly braces ({) and also specify the type of the value to be returned.

Example without return type declaration


function AddNum ($n1, $n2) {
    return $n1 + $n2;
}
$num1 = 10.6;
$num2 = 11;
echo AddNum ($num1, $num2); 
 

Output:


21.6

In the above example we can see that we have passed an integer and a float number as the arguments and the function have returned a float automatically and suppose we require an integer value to be returned in this case we use the return type declaration.

 

Example without return type declaration


function AddNum ($n1, $n2): int {
    return $n1 + $n2;
}
$num1 = 10.6;
$num2 = 11;
echo AddNum ($num1, $num2);

 

Output:


21

In the above example we can see that it used the same program but just added the return type declaration of the function. By doing so we get the return value of the function as an integer value.

Recursion in PHP

Recursive functions are those functions that call the same function within the function itself. It is also called recursion.

Recursion in PHP


function countNum ($n) {
    if ($n <= 10) {
        echo "$n ";
        $n++;
        countNum ($n);
    }
}
$num = 1;
echo countNum ($num);
 

Output:


1 2 3 4 5 6 7 8 9 10

In the above example, we can see that we have called the function only once outside the function and all the other calls of the function are done inside the function itself.