References Flashcards
What are references in PHP?
A means to access the same variable content by different names. In PHP, variable name and variable content are different, so the same content can have different names. They’re like hardlinking in the Unix filesystem.
Describe assigning by reference.
$a =& $b;
In this case, it means that $a and $b point to the same content. They are completely equal. $a is not pointing to $b or vice versa.
What happens when you assign, pass, or return an undefined variable by reference?
The variable will get created.
Does “new” return a value or a reference?
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 happens if you assign a variable with references in a foreach statement?
The references are modified too.
How can items of an array be made to behave like references?
By prefixing the array element to add with an ampersand.
How can you pass a variable by reference to a function?
In the function definition, the passed variable can be preceded by an ampersand.
What can be passed by reference?
Variables, new statements, and references returned from functions.
When you unset a reference, is variable content destroyed?
No.
Should you use return-by-reference to increase performance?
No. The engine will automatically optimize this on its own.