Custom Functions in PHP |Call by Reference and Call by Value Functions in PHP

What is functions in PHP | How to use functions in PHP | Syntax of functions

In this tutorial you will learn how to create your own custom functions in PHP.

PHP Built-in Functions

A function is a self-contained block of code that performs a specific task.

PHP has a huge collection of internal or built-in functions that you can call directly within your PHP scripts to perform a specific task, like gettype()print_r()var_dump, etc.

Please check out PHP reference section for a complete list of useful PHP built-in functions.

PHP User-Defined Functions

In addition to the built-in functions, PHP also allows you to define your own functions. It is a way to create reusable code packages that perform specific tasks and can be kept and maintained separately form main program. Here are some advantages of using functions:

  • Functions can be used to reduce the repetition of the code.
  • Functions make the code much easier to maintain.
  • Functions make code to easier to error free.
  • Functions can be used in different applications.
  • Functions can be use for regular tasks.

Defining and Calling of the Function

Syntax

function myFunctionName(){
    // Code to be executed
}

The declaration of a user-defined function start with the word function, followed by the name of the function you want to create followed by parentheses i.e. () and finally place your function’s code between curly brackets {}.

Functions with Parameters

function myFunc($oneParameter, $anotherParameter){
    // Code to be executed
}

PHP Function Call by Value

PHP call by value means function calling is done by a value. in such cases, the values passed to the functions cannot be changed. For example:

<?php 
function MyPhp($num1){
} 
$var= 10; //<------------variable is assigned a value. 
MyPhp($var); //<---------------function will call variable value i.e.10. this value will be sent to 
echo $var; //$num1. Then user can use this value anytime later in a function.
?>

Output

10

So, the value10 will only be stored in $num1. If the user defines some other value in $num1. Suppose, $num1= “hello”, The function will not change its value and will only use its previous value i.e. 10.

See the below example:

<?php 
function MyPhp($num1)
{
    $num1 = “hello”;
}
$var = 10; //<------------variable is assigned a value.
MyPhp($var); //<---------------function will call variable value i.e.10. this value will be sent to
echo $var; //$num1. Then user can use this value anytime later in a function.
?>

Output

10

Explanation: This will happen because both $var and $num1 are different variables. As we have already called $var value to $num1, we cannot replace this value with the new value. because we are trying to get value 10 which is assigned to $var, not $num1. That is why $num1 will not take “hello” but takes 10 as a value.

Another example to understand this code:

<?php 
function sum($num)
{
    $num .= "<br>hello";
}
$s = 10 + 21.05;
sum($s);
echo $s; 
?>

Output

31.05

PHP Functions Call by Reference

PHP call by reference functions values does not pass to the variables. It is the location or the address that passes to the function Rather they will accept its address at where the value is stored in a memory location.

What is an address of a variable?

When we declare a variable name e.g. $a. this variable will hold a space in the computer memory location to store its values. Then this variable will be assigned some memory address. This is the address by which this variable will be identified. These are called the addresses of the variables in a computer memory location.

Now coming back to the function call by reference.

In PHP, the addresses are passed to the variables by (&) sign. E.g. MyPhp (&$num1).

So, in these cases when addresses are passed to the variables rather than their values, the user can assign different values and the variable will take the new value.

Example 1:

<?php 
function MyPhp(&$num1)
{
    $num1 = “hello”;
}
$var = 10;
MyPhp($var);
echo $var; 
?>

Output

hello

In this code, we have a prefix (&) to variable $num1. Now, because of the (&) sign, variable $num1 will not take 10 as a value but “hello” as its value. Because $num1 has the address of the variable $var not its value. So, by putting the (&) sign, only the address of the variable will pass to $num1, not its value which is 10. Now, when the user will apply the echo statement, the word “hello” will be printed as an output.

Example 2: Now add (&) sign

<?php 
function sum(&$num)
{
    $num .= "<br>hello";
}
$s = 10 + 21.05;
sum($s);
echo $s; 
?>

Example 3:

<?php 
function first($num)
{
    $num += 2;
}
function second(&$num)
{
    $num += 10;
}
$number = 5;
first($number);
echo "original number is $number<br>";
second($number);
echo "original value is $number<br>"; 
?>

output

original number is 5
original value is 15

PHP Variable Functions

PHP variable Functions are used to assign a function name to a variable. This is another way of defining functions.

Syntax:

Function functionName(){
Statements
}
$variableName= “functionName”;
//function calling
$variableName();

Example Program:

<?php 
function first($name)
{
    echo "hello $name";
}
$fname = "first";
$fname("abc.in"); 
?>

Output

Hello abc.in

Example 2:

Assigning variable to a function: We can also define a function in a variable name.

<?php 
$hello = function ($name) {
    echo "hello $name";
};
$hello("abc.in"); 
?>

PHP Recursive Functions

PHP recursive function means when a function calls itself. It means when we call a function within a function is called a recursive function.

Syntax:

Function hello() {
//Statements
Hello(); //function calling itself
}
Hello ();

Example 1:

<?php
function display($number)
{
    if ($number <= 5) {
        // code...
        echo "$number<br>";
        display($number + 1);
    }
}
display(1); 
?>

Output

1
2
3
4
5

Explanation of Program:

  • The function will start from number 1 when it will be called.
  • Then this will be returned to the if statement to check its condition.
  • Repeating it cycle all the numbers will print till 5 and the program will then terminate.

Example 2:

Print factorial number.

<?php 
function factorial($number)
{
    if ($number == 0) {
        return 1;
    }
    return $number * factorial($number - 1);
}
echo factorial(5); 
?>

Output:

120

What is a factorial number?

For example, the factorial number of 5 is 120. How? 54321 =120. The factorial number of 3 is 6. How? 321 =6

Difference between PHP Function Call by value and Call by Reference.

Call by valueCall by reference
Variable values do not change.Variables values change.
Functions pass values to variablesFunctions pass addresses to variables.
Syntax: function_name($variable)Syntax: function_name(&$variable)

Custom Functions in PHP |Call by Reference and Call by Value Functions in PHP
Show Buttons
Hide Buttons