Does the difference between calling by value and calling by reference in php, and also $$ mean?

(1) I want to know what is the difference between calling by value and calling by reference in php . Does PHP work by call by value or by reference?

(2) And also I want to know what you mean by $$ in php

For instance: -

 $a = 'name'; $$a = "Paul"; echo $name; output is Paul 

As shown above, which means u, meaning $$ in PHP.

+6
source share
4 answers

$$a = b; in PHP means "accept the value of $a and set a variable whose name is equal to the value of b ".

In other words:

 $foo = "bar"; $$foo = "baz"; echo $bar; // outputs 'baz' 

But yes, look at the link to the PHP symbol .

As for the call by value / reference - the main difference between them is whether you can change the original elements that were used to call the function. Cm:

 function increment_value($y) { $y++; echo $y; } function increment_reference(&$y) { $y++; echo $y; } $x = 1; increment_value($x); // prints '2' echo $x; // prints '1' increment_reference($x); // prints '2' echo $x; // prints '2' 

Note that the value of $x does not change to increment_value() , but changes to increment_reference() .

As shown here, whether a call by value or a call by reference is used depends on the definition of the function being called; by default, when declaring your own functions, it is the callsign (but you can specify "call by reference" with the & sigil).

+11
source

Define the function:

 function f($a) { $a++; echo "inside function: " . $a; } 

Now try calling it by value (usually we do this):

 $x = 1; f($x); echo "outside function: " . $x; //inside function: 2 //outside function: 1 

Now let's redefine the function to pass the variable by reference:

 function f(&$a) { $a++; echo "inside function: " . $a; } 

and call him again.

 $x = 1; f($x); echo "outside function: " . $x; //inside function: 2 //outside function: 2 

You can pass a variable by reference to a function so that the function can modify the variable. More details here .

+6
source

This means $ ($ a), so it matches the name $ (since $ a = 'name'). More explanation here. What does $$ (dollar or double dollar) mean in PHP?

0
source

Calling by value means passing the value directly to the function. The called function uses the value in the local variable; any changes in it do not affect the original variable.

Calling by reference means passing the address of the variable in which the actual value is stored. The called function uses the value stored in the passed address; any changes in it affect the original variable.

0
source

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


All Articles