CHAPTER 27

image

References

A reference is an alias which allows two different variables to write to the same value. There are three operations that can be performed with references: assign by reference, pass by reference and return by reference.

Assign by reference

A reference is assigned by placing an ampersand (&) before the variable that is to be bound.

$x = 5;$r = &$x; // r is a reference to x$s =& $x; // alternative syntax

The reference then becomes an alias for that variable and can be used exactly as if it was the original variable.

$r = 10; // assign value to $r/$xecho $x; // "10"

Pass by reference

In PHP, function arguments are by default ...

Get PHP Quick Scripting Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.