A variable is something used to represent something else. If you think back to math class you will remember having problems like 5+x=7. In this equation x is the variable. We use a lot of variables in programing. Assigning a variable is very easy.
<?php $t= "This is some text"; print $t; //prints the string represented by $t $num = 12345; print $num; //prints the number represented by $num ?>
Look at the example above. You will see that we have set a variable equal to a string (this can be made of text and numbers) and to a number. You will also notice that our variables both start with a dollar sign. Every variable in PHP will have a dollar sign in front. You must also remember that variables are case sensative meaning $variable and $Variable are not the same thing. Variable names must start with a letter or an _underscore, they can not start with a number.
$num = 1234; //this is a legal variable name $123 = 123; //this is an illegal variable name $_num = 123; //this is a legal variable name
You can also see from our example that you can use Print or Echo statements to display variables back. When we do this, we do not need to put the variable in quotes, however we can. You can also put more than one thing in a print or echo statement by using a period to separate them, let me give you an example of these things:
print “I like the number $num”; //prints the number right inside the string echo $num . “ “ . $t; //echos the number, a space, and then the string
Directly assigning variables like this might not seem to have an obviously use to someone learning programming for the first time so let me give you some examples of how variables are very useful.
First let’s look at something simple that a lot of programs have, a preferences file. You may let the user set their own preferences such as the colors of your program, the size it runs at, or turning on or off different features. When the user sets this information, you don’t want them to have to change it in ever file that uses it, that would be far too time consuming. Instead this information is just changed once, in the preferences file. The variables holding their choices are then shared across all of the files of your program.
Another such use is when you are pulling lots of information from a database. Let’s say for example you wanted to print a list of final exam grades, you might do something like this:
Echo $name . “earned a grade of ”. $grade;
You will then pull names and grades from you database, and one by one they will be set equal to name and grade. As it loops through all the names and grades they are printed out. You only need to define the structure once, because the variables can change and the process can repeat. We will learn about arrays and databases in a later lesson.
So to summarize, all variables start with a dollar sign, they are case sensitive, and they must start with a letter or underscore. You can echo or print them inside or outside of quotes. Variables can hold different types of information. Variables are assigned using an equals sign.
Stay tuned for lesson 3 (coming soon)!