While learning the basics of PHP variables, constants and scope from “PHP The Good Parts” (MacIntyre, Peter B. PHP: The Good Parts. USA: O’Reilly, 2010), I wrote a simple page about favorite colors. Let’s break it on down — and then take a look at the less than exciting, but educational, page in action!
First, we start with the code that must exist at the top of every PHP page.
<?php
Next, let’s write a heading on the page using echo
, and throw in some comments to see how those are done in PHP. The multi-line comment style is also used in Javascript.
echo "<h2>PHP: Variables, Constants and Scope</h2>"; # Here is a single-line comment /* Here is a mult- line comment */
Now, we’ll create our first variable. It will hold a string that represents my favorite color.
Let’s include some text onscreen to explain that we are doing this first. Notice how we concatenate the strings of HTML output with a dot. We add in break tags, <br>
, where needed to move down a line in our HTML output. We also use backslashes to escape text in our output that may be interpreted as code and quotes within other similar quotes.
Don’t forget too, we must start each variable with a $
.
echo "Let's create a variable called \$favColorToday and assign it the string \"purple\". Then we will echo it." . "<br />"; $favColorToday = "purple"; echo "My favorite color today is: " . $favColorToday . "<br /><br />";
Well my favorite color today may be purple, but tomorrow, I think it will be red. Let’s create a second variable to store my favorite color tomorrow.
echo "Let's create a second variable called \$favColorTomorrow and assign it the string \"red\"." . "<br />"; $favColorTomorrow = "red"; echo "Perhaps my favorite color tomorrow will be: " . $favColorTomorrow . "<br /><br />";
Nah, I think my favorite color tomorrow will still be purple. How can we show this in our page? Variables can be assigned to other variables in the common way where the first variable takes on the value of the variable it is assigned. There is also a way to assign one variable to another so that they will be linked to each other in a special way. This is called an assignment by reference. We must place a &
in front of the variable being assigned to accomplish this. For now, it will just seem that we assigned our favorite color today to our favorite color tomorrow variable, nothing remarkable, but in a bit, we will see how the reference concept comes into play.
echo "Now we'll assign \$favColorToday to \$favColorTomorrow by <b>reference</b>." . "<br />"; $favColorTomorrow = &$favColorToday; echo "On the other hand, perhaps my favorite color tomorrow will still be: " . $favColorTomorrow . "<br /><br />";
It’s time to learn about variable scope. We have stored our favorite color today in a variable in our main document. What if we had a function that also had a variable of the same name and we assigned it a different color? Would our favorite color today change back in the main document? Let’s make a function and find out.
At the top of the document after our opening PHP line, we will add a function. Then we will call it next in our code.
function favColor() { $favColorToday = "green"; echo "My favorite color today inside favColor() is: " . $favColorToday . "<br />"; }
Back to our main code section…
echo "Now let's play with <b>scope</b> and see what a second \$favColorToday defined inside the function favColor() is..." . "<br />"; favColor(); echo "Yet outside of favColor(), \$favColorToday is still: " . $favColorToday . "<br /><br />";
As we’ll see in the output of the actual file, our variable within the function can be assigned “green”, as it is only defined within the scope of that function, where our original variable in our main document stays “purple”. This differs from Javascript where a variable defined in the main document would be recognized within a function on the page.
Now, back to that variable defined by reference. What does this mean? It means in our main document, our favorite color today and tomorrow variables are now linked. We change one, it affects the other. Let’s see this in action.
echo "Now change \$favColorTomorrow to green." . "<br />"; $favColorTomorrow = "green"; echo "My favorite color today, outside of favColor(), is now: " . $favColorToday . "<br /><br />"; echo "Now change \$favColorToday back to purple." . "<br />"; $favColorToday = "purple"; echo "My favorite color tomorrow is now also: " . $favColorTomorrow . "<br /><br />";
Wow, by changing our favorite color tomorrow to “green”, it actually changed our favorite color today in the main document. Then changing our favorite color in the main document back to “purple” changed our favorite color tomorrow as well. Interesting…
Let’s look at one more example of reference variables, this time involving a function which we’ll include with the others. Notice how the parameter is preceded by the reference character &
in the function.
function secondFavColor(&$secondFavColorToday2) { $secondFavColorToday2 = "blue"; echo "My second favorite color today inside secondFavColorRef() is: " . $secondFavColorToday2 . "<br />"; }
We will also create a new variable back in the main section. We are going to send this variable to the function by reference, so changing the parameter variable received in the function will change our variable back in the main document.
echo "Let's create a new variable \$secondFavColorToday to store my second favorite color today, grey. We will pass it by reference to a function, where we will change it to blue." . "<br />"; $secondFavColorToday = "grey"; secondFavColor($secondFavColorToday); echo "My second favorite color today, outside of secondFavColor(), is now: " . $secondFavColorToday . "<br /><br />";
Lastly, let’s check out constants. These types of variables in PHP remain the same no matter where they are referenced within the document or functions, i.e. they have global scope, and they cannot be modified. Constants act more like a variable defined in the main document of a Javascript file in terms of scope. We create constants a little differently, by using define
, and they have a standard naming scheme that they tend to follow of all uppercase, which allows them to stand out (in my humble opinion). We will now create a constant in the main document and access it within a function. We will see that it is accessible within that function.
First, we create the function back up with our other functions.
function favSysColor() { echo "Calling favSysColor(), and SYS_COLOR is still: " . SYS_COLOR . " in this function as it's scope is throughout the main document and all functions."; }
Back in the main section of page, we will include the remainder of our code, including our closing PHP line ?>
.
define("SYS_COLOR", "orange"); echo "SYS_COLOR was defined, and this <b>constant</b> is: " . SYS_COLOR . " in the main document. " . "<br />"; favSysColor(); ?>
There you have it! Time to see the results of our efforts: