Passing Arguments by Reference in PHP

Passing arguments by reference in php. By reference, pass Passing the reference of an argument in the calling function to the corresponding formal parameter of the called function, also known as pass by address, involves making a copy of the address of the real parameter in memory, where the caller and the called utilize the same parameter variable.

<?php
function add(&$value)
{
$value += 5;
}

$num = 2;
add($num);
echo $num;
?>

 

Output:

7

Passing Arguments by Reference in PHP

The term “pass-by-reference” refers to passing the reference of a calling function argument to the corresponding formal parameter of the called function. The called function can change the value of the argument by using the reference passed in. 

How to pass arguments by reference is demonstrated in the following example. When passing variables by reference, use the & (ampersand) symbol before the variable argument. For instance, function(&$x ). Because both the global and function variables are defined by the same reference, their scope becomes global.

$ in PHP Program

In PHP, variables are denoted by the symbol $. PHP variables are dynamically typed, which means their type is determined by what is assigned to them.

Function in PHP Program

In PHP, a function is a reusable piece of code that performs a specific action. It receives user input in the form of parameters, performs certain actions, and returns the output. When called, functions can either return values or simply perform an operation without returning any value.