Got past the opening page, eh? In this section of the tutorial we’ll learn how to use variables in our PHP scripts and, most importantly of all, why we use them.
VariablesUsing variables in your PHP documents is very easy, this is how variables look:
A variable starts with a dollar sign ($) followed by whatever you want to call it, variable names can consist of letters, numbers and the _ character. However, do not start variable names with a number.
What Do They Do?Variables store a value, whether it be a sentence (string), number (integer) or a letter (char). You assign a value to the variable using the assignment operator (=).
We just assigned the value of 1 to the variable $variable. This $variable is now 1, so if we printed it out, it would print 1.
We can assign a variable a number, a letter or a sentence.
If we then used echo to print out these variables, the output would be whatever the variable contains. Variables are essential and you must learn how to use them, let’s take a look at the semantics of using the echo statement with variables.
Echo and VariablesUp to now we have only used the echo statement with a string and with a single variable. How you use the echo statement, or more specifically if you use single quotes, double quotes or no quotes at all depends on what you are printing out.
Single QuotesWe use single quotes when we want to print out a something that contains no variables (or arrays, we’ll get to them next, ignore for now), take a look:
That works fine, it will print out the string.
No QuotesIf we are printing out a variable, and nothing but a variable, we don’t need to surround it by quotes at all, take a look:
Double QuotesWhat if we want to print out a variable within a string or something? Then we use double quotes, take a look:
If a variable is to be printed out amongst the words of a string then the whole thing must be contained within double quotes. If we wish to interpolate a variable into a sentence this is how we do it. Why, though? What if we used single quotes?
Not what we want, is it? We want the value of the variable to be printed, not the name along with the dollar sign, this is the difference between single quotes and double quotes. Use single quotes when just echoing out a normal string, if it contains a variable, use double quotes.
This applies not only to echo, but with everything in PHP, when assigning a variable to another variable we use the same rules.
$myname would contain
That’d work as we want, remember that, use quotes only if you need them, and use the correct ones.
An AlternativeAnother method we can use when wanting to print out a variable is jumping out of the quotes, take a look:
The latter of the two examples may not make grammatical sense but it shows you how you use the periods to attach the variable into the string. Close the quotes, type a dot, type the variable and then, if you need to, you can type another dot and open the quotes again. Have a play around with variables, echo and assignments, you’ll see what I’m talking about.
It wasn’t really necessary to show that last method, but if I didn’t I probably would have gotten a comment at the bottom by some PHP “guru”:
<sneer>This tutorial sucks, you didn’t even mention the dot thing, hahaha call yourself a PHP coder? Stop polluting my screen with this crap!</sneer>
Indeed. So there you have it.
But, why?Well … the reason we used variables in the above examples is still unclear. Why not just write “My name is Jester”? Well what if we wanted to use the value “Jester” through out a script many times? We could just type “Jester” in every place we wanted to use it. What if I changed my name, though? I’d have to go through and change every instance of “Jester” to my new name, if I had it stored in a variable I could just assign the variable a new value and it’d take effect in every instance I used it. This is the power of using variables.
Another point is, what if a user submits the value of “10” to a script? You can’t just store “10” on its own in a program, can you? You need to put it somewhere, and what better place than a variable?
Still unclear? Don’t worry about it, at this point in my PHP lessons I was very hazy too. You got this far, keep reading and you’ll see why we use them. Let’s go on and look at Arrays.
No Comments for this page.