More Math? x + 3 = 5 Kind of Variables?
I wrote about variables in the PHP 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.
I know that we haven’t covered functions yet, but for those that are interested and familiar with them, you might enjoy the next segment.
When passing an argument to a function, we can pass the argument by value, or by reference. The same approach is taken by pre-pending the parameter declaration with the & symbol. When passing the argument by value, a copy is made. Any modifications to the value ends when the function ends. When passing an argument by reference, the modification is made to the variable that’s outside of the function.
To illustrate this, we’ll create a new variable $name. We’ll store the value “Dino” and pass that value to the function displayName($name). That function will modify the variable inside of the function body and change the value to “Frank.” When we echo out $name outside of the function, the value that’s displayed is still “Dino.”
If we pass the $name variable to displayNameByRef(&$name) and modify the $name value inside the function, we get a different result than before when we echo the $name variable outside of the function. If the function changes the value of $name from “Dino” to “Frank,” and the variable is echoed outside of the function, the value that’s displayed is “Frank.”
PHP Tutorial Series
Continue your learning with these articles
How Many Dimensions Deep Have you Been?
PHP – P11: Multidimensional Arrays
What are PHP multidimensional arrays? Just think of an array within an array. In other words, it’s an array that stores arrays as elements. Those elements that are arrays themselves can have arrays as elements.
More Math? x + 3 = 5 Kind of Variables?
If you’re familiar with other programming languages, an associative array is pretty much a dictionary. Instead of accessing the array by using an index value, you’ll access it by using a key.
VARIABLE VARIABLES? WAS THAT A TYPO?
PHP variable variables are dynamic variables, which mean that you can generate variable names dynamically. Prepend a dollar sign to a string, the string becomes a variable.