PHP 7.x — P12: Variables. I wrote about variables in the…
By: Dino Cajic
Tags: PHP, Web Development, Programming, Computer Science, Software Development

I wrote about variables in the Variables Intro article, but I’m going to dedicate the next two articles explaining variables even further. Three articles on variables? Yes. There’s that much material that needs to be covered.

Recap: In PHP, variables start with a dollar sign, followed by either the underscore or a letter, followed by a series of letters, numbers, and/or underscores. They are case sensitive, so $awesome, $Awesome, $aWesome, and $AWesome are all different variables.

PHP allows you to assign a value to a variable and then reassign another value with a completely different data type; most other programming languages do not allow this.

<?php

$x = "Dino";

$x = 1;

$x = true;

?>

So far, pretty standard stuff. But I want to dig deeper and explore variables from a Computer Science perspective. My goal is for you to understand what’s happening behind the scenes.

Let’s say that we wanted to copy a variable. There are two different approaches to this: Copy by value and copy by reference.

What does it mean to copy a variable by value? To answer this question, we’ll create a new variable.

<?php

$name = "Dino Cajic";

?>

“Dino Cajic” is a value that’s stored in memory. We’ll say that it’s stored at memory location 0x12FF. The variable $name points to that memory location. When you call the $name variable, it retrieves the value out of that memory location, which is “Dino Cajic.” This is an oversimplification of the actual process.

If we create a new variable, $name_2, and we want to copy the value “Dino Cajic” by setting $name_2 = $name, what happens in memory?

<?php

$name = "Dino Cajic";

$name_2 = $name; // $name_2 = "Dino Cajic"

?>

PHP looks at the $name variable. It goes into the memory address that it’s pointing to and retrieves the value. So, it looks at memory address 0x12FF and retrieves the value “Dino Cajic.” It then assigns the value “Dino Cajic” to $name_2. How? By storing the value “Dino Cajic” in a new memory location, 0x34FD, and pointing the variable $name_2 to that new memory location. The variable $name now points to 0x12FF and the variable $name_2 points to 0x34FD. The values are the same, but the locations in memory are different.

If we modify the variable $name_2 to store “Stephen Johnson,” only the value inside memory location 0x34FD is modified. The value for $name is “Dino Cajic” and the value for $name_2 is “Stephen Johnson.” This is what’s meant when we say “copy by value.”

<?php

echo $name; // Display: Dino Cajic

echo $name_2; // Displays: Stephen Johnson

?>

Let’s tackle “copy-by-reference” next. We’ll create a new variable, $name_3, and this time, we want $name_3 to point to the same memory location as $name. Both $name and $name_3 need to point to memory location 0x12FF.

<?php

$name_3 = &$name;

?>

The & symbol is what tells PHP that we want to copy by reference. The variables technically just store memory locations, so we just copied the memory location from $name, which is 0x12FF, to $name_3. Since both $name and $name_3 now point to the same memory location, modifying either of the values modifies the value for both.