PHP Variables
So far we have looked at the basic anatomy of PHP code and understand how it is processed (server side) to generate HTML so that our users can view outputs within their browser (client side). We explored how we could generate dynamic content using the echo command, and saw that content can come in a variety of different formats – we looked at strings and integers ( a type of number). We also saw how we could concatenate content, or join different pieces of content together, to form more complex output that can come from different places. In this lesson we will explore variables.
What Are Variables
We use variables a way to reference a stored value, a value that can change or vary. One way to think of a variable is a label for a drawer that contains some object. The name on the label can be anything you want and the object within the drawer can also be anything you want – but doesn’t it makes sense to have the label relevant to the object inside it?
For example, if the drawer contains red shoes then the label on the drawer should be something like “red_shoes” rather than “Var1” – it makes it easier for us, the developers, to quickly understand what is contained within the drawer. The contents of the drawer can also change. For example, we could add another pair of shoes or remove all of the shoes and replace them with hats – we van vary the contents of the drawer.
Now, if our variables were actually drawers we could write anything on the label and not worry about rules but in PHP there are some rules that we need to observe.
Naming Variables
To start, every variable must start with a dollar sign ($). This will be very useful to you when you are reading code, you can quickly identify variables in a sea of code.
Once you have started the variable name with a dollar sign you must follow that with a letter or underscore, you cannot use a number or other non-letter character.
Once you have satisfied the first two rules you can then include letters, numbers, underscores and dashes within the name of your variable BUT you cannot have ANY spaces within your variable name. Once more, there are NO SPACES in variable names.
Finally, variable names are case sensitive. This means that $Var1 is a totally difference variable from $var1 – notice the V and v?
To summarise, variable names:
- start with a $
- are then followed by a letter or an underscore
- can contain letters, numbers, underscores or dashes
- cannot have any spaces
- are case sensitive
So what passes for a legal variable name?
- Both $Product and $product are perfectly good variable names. They are of course different variables because one is spelt with an upper case P and the other with a lower case p.
- $firstNumber is also a legal variable name. Here, we are using camel case to name it easier to read the name of the variable that uses two or more words – camel case is where you upper case the first letter of the second and subsequent words in a variable name. For example $myFirstVariableName uses camel case but $myfirstvariablename does not – can you see that the first is easier to read?
- $my_variable and $my-variable are also legal variable names but the latter name, the one that uses a dash, is less popular since a dash (-) looks identical to a minus sign (-) so we recommend that you stick to underscores within your variable names.
- $variable1 and $variable2 are also legal variable names, as long as we don’t use a number at the beginning of the variable name we can use as many numbers a we wish.
- $_shoe and $__shoe are legal names but using multiple underscores can make it difficult to know at a glance if there are one, two or more underscores present so we recommend that you avoid underscores all together. In addition to potential confusion about the number of underscores, PHP uses underscores for some of its own special variables that we will discuss in a later lesson. So let’s avoid using underscores as a starting character for all of our variables.
There is one last thing that you need to know about variable names. PHP has some reserved words that it uses for a variety of functions that we should avoid. You will find the latest and a complete list of reserved words here: php.net/manual/en/reserved.php.
Now that we have a good understanding of what variables are, why we might use them and how to name them let’s learn how to create them.
Creating Variables
To create a variable we need to first state the name of the variable and the assign it a value. Let’s call our first variable my var, and if we apply all of the naming rules we learnt earlier in this lesson (starts with a $, no spaces, underscores are ok) we should called it something like $my_var – see how that works?
Once you form the variable name that follows the rules we than need to assign a value to it, let’s assign the integer 50 to out variable:
<?php $my_var = 50; ?>
And that’s it. We have created a brand new variable called $my_var and assigned the integer 50 to it. Note the semicolon at the end of that variable assignment – those rules still apply!
Now, and for the lifetime of this script unless we change it, the variable $my_var will point to the integer 50 and we can call this variable as may times as we like.
Let’s do that, let’s echo the value of $my_var:
<?php $my_var = 50; echo $my_var; ?>
Try that and see what you get. You should only see the value that $my_var points to and not the string $my_var, you should only see the number 50. Check your code if you see anything else in your browser window. If you see the string “$my_var” or “echo $my_var” you may have forgotten to insert your PHP script within PHP tags (<?php ?>). Don’t do any further until you are seeing the right result in your browser.
Now that we have successfully created a variable, assigned it a value and echoed that value to a browser let’s see if we can change, or vary, the value associated with this variable:
<?php $my_var = 50; echo $my_var; echo "<br>"; $my_var = 100; echo $my_var; ?>
Here we have added another assignment to the variable $my_var, this time assigning 100 to it. We have also echoed a <br> tag to separate the two variable echos. Now when we view the output in a browser we can see that both values of $my_var have been output, the initial value of 50 and the later value of 100. This means that we can change the value associated with a variable by simply reassigning a new value to it.
We are not limited to numbers, we can assign other data types to our variables also. Let’s try using a string:
<?php $my_var = 50; echo $my_var; echo "<br>"; $my_var = 100; echo $my_var; echo "<br>"; $my_other_var = "This is my first variable using a string!"; echo $my_other_var; ?>
Here we created a brand new variable called $my_other_var and assigned the string “This is my first variable using a string!” to it. By echoing that variable PHP has output the value associated with it just like it did with the earlier variable associated with integers.
So you can see that it behaves in exactly the same way. As long as the variable has a value associated with it we can retrieve that value by referring to the variable name. Also, we can change those values at any time by reassigning a new value to it.
Now that we have a basic understanding of creating variables, assigning and reassigning values to them let’s take a closer look at strings in the next lesson.