References Flashcards

1
Q

What are references in PHP?

A

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.

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

Describe assigning by reference.

A

$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.

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

What happens when you assign, pass, or return an undefined variable by reference?

A

The variable will get created.

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

Does “new” return a value or a reference?

A

Reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
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
6
Q

What happens if you assign 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
7
Q

How can items of an array be made to behave like references?

A

By prefixing the array element to add with an ampersand.

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

How can you pass a variable by reference to a function?

A

In the function definition, the passed variable can be preceded by an ampersand.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
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
10
Q

When you unset a reference, is variable content destroyed?

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
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