References Flashcards

1
Q

What are references in PHP?

A

A means to access the same variable content by different names. They are likened to Unix hardlinking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the three basic operations performed using references?

A

Assigning by reference, passing by reference, and returning by reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Using $a =& $b, is a pointing to b, or b pointing to a?

A

Neither. $a and $b are completely equal. $a and $b are pointing to the same place.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If you assign, pass, or return an undefined variable by reference, what happens?

A

It will get created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does ‘new’ return automatically?

A

A reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens if you assign a reference to a variable declared ‘global’ inside a function?

A

The reference will be visible only inside the function. You can avoid this by using the $GLOBALS array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What can ‘global $var’ be considered a shortcut for?

A

$var =& $GLOBALS[‘var’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

If you assign a value to a variable with references in a foreach statement…

A

…the references are modified too.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can expressions created with the language construct array() behave as assignment by reference?

A

By prefixing an ampersand to the array element to add.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why are references inside arrays potentially dangerous?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Are references pointers?

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

There is no reference sign on a function call - only on…

A

…function definitions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What can be passed by reference?

A

Variables, new statements, and references returned from functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Should you use return-by-reference to increase performance?

A

No. The engine will automatically optimize this on its own.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens when you unset a reference?

A

You just break the binding between variable name and variable content. It does not mean that the variable content is destroyed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Besides passing and returning by reference, what are other constructs that use references?

A

When you declare a variable as global $var, you are in fact creating a reference to a global variable. This is the same as:

Also, in an object method, $this is always a reference to the caller object.