Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Working with Variables and References

Since the guessing game in Chapter 2, you have written $x = $y without a second thought. It deserves one.

Write that line with an array, then with an object, and change the copy each time. With an array, the original stays put. With an object, the original changes too. Same line, two opposite behaviors, and the difference is not a detail of the engine. It is one of the most common surprises for people arriving from another language, and one of the most reliable sources of strange bugs for people who never had it explained. Get it backwards in your head, and one day a function will “not work” for no visible reason, right up until you understand this chapter.

Arrays behave as if every assignment made you a fresh, independent copy. Objects behave as if every variable holding one were just another name for the same thing. The first section looks at how PHP copies arrays, and at the trick it uses to make that cheap. The second covers the & that lets you share a variable on purpose, and the way objects are shared whether you ask or not. References are a sharp tool, useful in a few precise situations and a reliable source of confusing code when reached for out of habit, so you will also learn where they earn their keep.

The chapter closes on two smaller ideas that live next door: what a function can and cannot see of the variables around it, and how PHP takes out the memory it no longer needs. Neither takes long to learn, and both will come up again later in the book.