A function passed with an argument like "& $ x" displays a value, not an error, see code below

Below is the code that displays "15", why?

function zz(&$x){

$x = $x + 5;

}

$x = 10;

zz($x);
echo $x;

Explain, please

+3
source share
6 answers

you pass the value because the argument is not a direct value of the variable, but passing it by reference, so it gives you 15 as output.

Thank!

+3
source

It works as it was designed. Using &, you pass $xby reference, which means that everything that the function does with the variable will be done with the original $xone that is set to 10.

If you used

function zz($x)

$x 10, .

+6

, , , .

, , .

+2

a , $x . .

+1

$x , $x .

"&", , , , , .

.

+1

: .

Just change the original variable and return it again to the same variable name with the assigned new value.

+1
source

Source: https://habr.com/ru/post/1775848/


All Articles