5 Types of Functions in PHP with Examples

In this article we are going to cover functions in PHP, Types of functions in PHP, Advantage of PHP functions, Arguments in PHP, Function parameter in PHP

What is Function in PHP?

A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.

A function is block of code written in a program to perform some specific task.

PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task.

Two major types of functions in PHP:

1. Built-in function:

PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task. This functions are already coded and stored in form of functions.

2. User Defined Function:

We can declare and call user-defined functions easily. PHP allows our own customised function called the user-defined function. Using this we can create our own package of code and use it.

Advantage of PHP functions:

Code Reusability : If we have a common code that we would like to use at various parts of a program, we can simply contain it within a function and call it whenever required. It defines only once can be invoked many times, like in other programming languages.

Less Code : In PHP, it saves a lot of code don’t need to write the logic many times. By the use of function, you can write the logic only once and reuse it.

Easy to Understand : It is easier to understand the flow of the application because every logic is divided in the form of function.

Create a function in PHP:

Syntax:

function function_name(){
	executable code;
}

Example of simple function call in PHP:

<?php
 
function fun_demo()
{
    echo "This is FOSSTechNix";
}

fun_demo();
 
?>

Output:

This is FOSSTechNix
5 Types of Functions in PHP with Examples 1

Function Parameter or Arguments in PHP :

We can pass the information in PHP function through argument which is separated by comma. The information or variable, within the functions parenthesis are called parameters. These are used to hold the values executable during runtime. A user is free to take in as many parameters as he wants, separated with a comma(,) operator. These parameters are used to accept inputs during runtime.

Syntax:

function function_name($first_parameter, $second_parameter){
	executable code;
}

Example of Function Parameter or argument in PHP:

<?php
function fun_demo($num1, $num2, $num3)
{
    $add = $num1 * $num2 * $num3;
    echo "The Addition is $add";
}
fun_demo(2, 2, 2);
 
?>

Output:

The Addition is 8
5 Types of Functions in PHP with Examples 2

Default Values for Function parameter in PHP:

PHP allows us to set default argument values for function parameters. If we do not pass any argument for a parameter with default value then PHP will use the default set value for this parameter in the function call.

Example of default values function parameter in PHP:

<?php
 
function fun_demo($str, $num=12)
{
    echo "$str is $num years old \n";
}
fun_demo("Ram", 15);
fun_demo("Lakhan");
 
?>

Output:

Ram is 15 years old Lakhan is 12 years old
5 Types of Functions in PHP with Examples 3

Returning Values from Functions in PHP:

The return keyword is used to return value back to part of program.

Example of returning values from function in PHP:

<?php
 
function cube($num1, $num2, $num3)
{
    $number = $num1 * $num2 * $num3;
     
    return $number; 
}
 
$value = cube(4, 4, 4);
echo "Cube of 4 is $value";
 
?>

Output:

Cube of 4 is 64
5 Types of Functions in PHP with Examples 4

Parameter passing to Functions in PHP:

PHP allows two ways in which an argument can be passed into a function:

1. Pass by Value in PHP:

We followed to pass the arguments to the function with passed by value approach. We are following this practice because if the value of the argument within the function is changed, it does not get changed outside of the function.

2. Pass by Reference in PHP:

As it is already mentioned we can pass a variable by reference to a function so the function can modify the variable. To begin the process of passing the parameters passed by reference, prepend an ampersand (&) to the argument name in the function definition.

Example of Parameter passing to function in PHP:

<?php
function val($num) {
    $num += 4;
    return $num;
}
function ref(&$num) {
    $num += 5;
    return $num;
}
 
$n = 10;
 
val($n);
echo "The original value is still $n \n";
 
ref($n);
echo "The original value changes to $n";
 
?>

Output:

The original value is still 10 The original value changes to 15
5 Types of Functions in PHP with Examples 5

Conclusion:

We have covered In this article we are going to cover functions in PHP, Types of functions in PHP, Advantage of PHP functions, Arguments in PHP, Function parameter in PHP.

Related Articles:

Reference:

Functions of PHP official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap