PHP Functions

PHP Functions

  • PHP
  • 6 mins read

In this tutorial, you will learn what are PHP functions and how to use them.

What is a Function in PHP?

A function is a block of codes that together perform a task. Each function can take one more input in the form of the parameter and perform something and returns a value. PHP supports two types of functions, one is built-in functions, and another is user-defined functions.

What is a User-defined Function?

A User-defined function is a function which is defined by the user. A user-defined function can be executed by calling that function.

How to Create a User Defined Function in PHP?

In PHP, a function can be created through the keyword function followed by a function name. A function name can start with a letter or an underscore but not begin with a number and function body will start with an opening curly brace({) and will end with a closing curly brace(}).

The syntax of the User-Defined Function

function functionname
{
//block of codes to be executed
}

In PHP function names are not case sensitive.

Here we will give an example for defining a function:

function display()
{
echo "Hello India";
}

Here the above function "display()" is containing a code echo "Hello India". When display() function is called then echo "Hello India" will be executed.

Here is a PHP Program for Defining a Function and Calling The Function

<html>
    <body>
        <?php
function print1()
{
    echo "Welcome to India";
}
print1();//calling the function
        ?>
    </body>
</html>

Output:

Welcome to India

In the above program we have defined a function print1() and then called that function. After calling the function, then the function code will be executed and "Well come to India" will be displayed.

How to Pass Arguments to a Function in PHP?

Through arguments, we can pass information to a function. An argument is like a variable. Arguments can be specified after the function name, inside the parentheses and a comma separates them.

Here is an Example for Passing Arguments to a Function in PHP

function f1($a,$b,$c)
{
//code to be executed
}

Here $a,$b and $c are three function arguments, and they are separating by a comma.

Now we will write a program for defining a function print1() with an argument $a.

<html>
    <body>
        <?php
function print1($a)
{
    echo $a."<br>";
}
echo "The employess are:-<br>";
print1("John");
print1("Tarak");
print1("Mona");
        ?>
    </body>
</html>

Output:

The employees are:-
John
Tarak
Mona

In this program, we are passing an argument and calling the function with a string value three times. When this function is executed for three times, then three strings("John","Tarak","Mona")will be displayed on the web page.

Now we will give an example with more than one arguments:

<html>
    <body>
        <?php
function print1($a,$b)
{
    echo $a." ".$b;//code of function body
}
echo "The two colors are:-<br>";
print1("Red","Green");
        ?>
    </body>
</html>

Output:

The two colors are:-
Red Green

The user-defined function print1()in the above program, is containing two arguments here(one is $a, and another is $b). Two strings "Red" and "Green" are passing as values to the function arguments $a and $b in the time of function calling and then function body will be executed.

The following is a PHP program to display the maximum value of three numbers using a user-defined function

<html>
    <body>
        <?php
function max1($a,$b,$c)
{
    if($a>$b&&$a>$c)
        echo "The First number is maximum";
    if($b>$a&&$b>$c)
        echo "The Second number is maximum";
    if($c>$b&&$c>$a)
        echo "The Third number is maximum";
}
max1(90,80,70);
        ?>
    </body>
</html>

Output:

The First number is maximum

In this program, three values 90,80,70 are passing as values to function arguments. When a function is executed then condition of each if statement will be checked and based on satisfaction of one condition, "The first number is maximum" will be displayed.

How to Return a Value from a Function in PHP?

In PHP, a function can return a value through a return statement and return will stop the execution of the function and will send the value to the calling code.

Now we will write a program for using a return statement:

<html>
    <body>
        <?php
function max1($a,$b,$c)
{
    $d=$a+$b+$c;
    return($d); 
}
$k=max1(90,80,70);
echo "The sum of three numbers is:-".$k;
        ?>
    </body>
</html>

Output:

The sum of three numbers is:-240

This program here is containing a user-defined function with three parameters, and the function body is containing a return statement. When a function is called, then the function will return a value and the value will be assigned to $k. And finally, the value of $k will be displayed.

PHP Program Example to Display The Factorial of a Number

Factorial of a number is the value of multiplication from 1 to that number.

<html>
    <body>
        <?php
function factorial($a)
{
    $fact1=1;//initialization
    for($i=1;$i<=$a;$i=$i+1)
    {
        $fact1=$fact1*$i;
    }
    return($fact1);
}
$k=factorial(5);//here the function is returning the factorial of 5 to the variable $k
echo "The factorial of 5 is:-".$k;
        ?>
    </body>
</html>

Output:

The factorial of 5 is:-120

In this program the function body is containing here a for loop, that is running from 1 to $a(here five is passing a value to ($a) in the time of function calling), and function is returning a value. When the function is executed, then it will return a value, and that value will be printed through an echo statement.

What is the Default Argument Value of a user-defined Function in PHP?

In PHP, the default argument value is a value which will be passed as a value to a user-defined function in the time of function definition. When that function is called without any argument, then the function will take the default value as an argument.

Now we will write here a function f1 with a default argument value 5:

<html>
    <body>
        <?php
function f1($a=5)
{
    echo $a."<br>";
}
f1();
f1(90);
        ?>
    </body>
</html>

Output:

5
90

In this program, we are calling the function f1 for two times. Firstly function is calling without any argument, so here the function will contain the default value five as an argument and then function body will be executed and will display 5. Secondly, the f1 function is calling with an argument as a value 90 and then the function will be executed and will display 90.