Help me understand the links and scope of PHP variables

Literature:

  • If you pass a variable to a function (for example, $ var), should it be a copy of the reference to the actual variable (such that its null value does not affect other copies)?
  • Or does he get a link to what is a new copy of the actual variable (such that setting it to zero destroys only its copy)?
  • If the latter, does it copy objects and arrays into memory? This seems like a good way to get rid of memory and CPU time, if that is the case.

I think I can figure out how to pass the link (e.g. & $ var) correctly, knowing how this works in the first place.

Application area:

  • What is a deal with local reach? Have I noticed correctly that I can declare an array in one function and then use this array in other functions called inside this function WITHOUT passing them to them as a parameter?
  • Likewise, declaring in a array a function called inside a function allows it to be available in the caller?
  • If this is not the case, does the inscription on the call stack work or something like every bloody thing that I understood about programming, do I need it?

PHP is a lot of fun .:(

+7
source share
3 answers

If you pass a variable to a function (for example, $ var), is it assumed that this is a copy of the reference to the actual variable (such that setting it to null does not affect other copies)?

Depends on function. And also, as you call it. Have a look at this example: http://www.ideone.com/LueFc

Or does he get a link to what is a new copy of the actual variable (such that setting it to zero destroys only its copy)?

Depends on function again

If the latter, does it copy objects and arrays into memory? This seems like a good way to lose memory and CPU time if that is the case.

He is going to save memory in order to use the link, of course. In php> 4, it always uses a reference for objects, unless you specify otherwise.

What is a deal with local reach? Have I noticed correctly that I can declare an array in one function and then use this array in other functions called inside this function WITHOUT passing them to them as a parameter?

No, you canโ€™t.

Similarly, declaring in an array in a function called inside a function allows it to be available in the caller?

No, it is not.

If not, does the inscription on the call stack work or something like every bloody thing I understood about programming, do I need it?

If you want to use a variable from outside the function, you must write global $outsidevar before using it

+2
source

Regarding your first set of questions:

 foo($a); function foo($b) { echo $b; } 

In this case, $a will not be copied to the new variable $b , only because it is passed by value.

This is because PHP uses the concept of copy-on-write. PHP will not copy the contents of a variable unless they are modified. Instead, PHP will increment the refcount property of the existing "zval" $a .

Well, the whole point is not that trivial, but to answer your question: No, it does not copy the variable if you do not write it to the function, and no, you do not save the CPU and memory using the Help. In most cases, a link does not change performance at all, but in the worst case, it actually degrades it (because if a variant of the is_ref variable already exists and a link is created, the value of the variable must be copied to get zval with is_ref and one without). Code optimization with links is not suitable.

+1
source

if the function argument is defined as "function my_function ($ variable) {}", then you get a copy of the variable, and any changes made to the variable inside your function will not be available to the calling function. you can pass the variable by reference, adding an ampersand to the argument when defining your function, and thus, any changes made to the variable will be saved for the calling function, that is, "function my_function (& $ variable) {}"

 function myfunction($var) { $var = 'World'; } $var = 'Hello'; myfunction($var); echo $var; // 'Hello'; 

Passing a variable by reference

 function myfunction(&$var) { $var = 'World'; } $var = 'Hello'; myfunction($var); echo $var; // 'World' 
+1
source

All Articles