References Flashcards
What are references in PHP?
A means to access the same variable content by different names. They are likened to Unix hardlinking.
What are the three basic operations performed using references?
Assigning by reference, passing by reference, and returning by reference.
Using $a =& $b, is a pointing to b, or b pointing to a?
Neither. $a and $b are completely equal. $a and $b are pointing to the same place.
If you assign, pass, or return an undefined variable by reference, what happens?
It will get created.
What does ‘new’ return automatically?
A reference.
What happens if you assign a reference to a variable declared ‘global’ inside a function?
The reference will be visible only inside the function. You can avoid this by using the $GLOBALS array.
What can ‘global $var’ be considered a shortcut for?
$var =& $GLOBALS[‘var’]
If you assign a value to a variable with references in a foreach statement…
…the references are modified too.
How can expressions created with the language construct array() behave as assignment by reference?
By prefixing an ampersand to the array element to add.
Why are references inside arrays potentially dangerous?
Doing a normal (not by reference) assignment with a reference on the right side does not turn the left side into a reference, but references inside arrays are preserved in these normal assignments. This also applies to function calls where the array is passed by value.
Are references pointers?
No.
There is no reference sign on a function call - only on…
…function definitions.
What can be passed by reference?
Variables, new statements, and references returned from functions.
Should you use return-by-reference to increase performance?
No. The engine will automatically optimize this on its own.
What happens when you unset a reference?
You just break the binding between variable name and variable content. It does not mean that the variable content is destroyed.