12 January 06
Beginner PHP Tutorial 2: Variables and Values
Variables are used across pretty much all programming languages since they are an easy way to represent areas of computer memory and data. If you know algebra or have programmed in another language then this will be extremely easy, otherwise you might need to focus a bit in this tutorial.
The Idea of Variables and Values
As I said before, areas in memory are represented by variables. Variables are names for these areas and the data contained within them. In PHP you don’t need to worry about how data is stored and taken from memory so much. You just need to understand that variables hold data (the value). So a variable called name could hold the value Jim. If you imagine a table for personal details for a bank, then you would have columns of something like ‘Name’, ‘Address’, ‘Phone Number’ and possibly ‘Email’. Under these columns you would have rows with data. You could look down the ‘Phone Number’ column. This would be like getting the value of the variable ‘phone_number’ although this moves slightly into the realm of arrays as there are often multiple records in the table. Don’t worry, I will explain them in their own tutorial.
Variables in PHP
So now you understand variables you can use them in PHP. All variables begin with a dollar sign ($). So:
$name = 'Jim';
assigns the value ‘Jim’ to the variable ‘name’. Also, you can assign the results of a calculation into a variable:
$result = 8 * 7;
...or you can use a variable in a calculation:
echo $number * $othernumber;
Variable Naming Rules
Variables can’t have any name, they must comply with certain rules:
- The first character of a variable name must be a letter or an underscore (_).
- After the first character the characters must be either letters or numbers or underscores. So $system_Name65 is valid (I think).
Variables in Strings
If you want to place a variable into a string you must either use double quotes (“”) which can contain both text and variables or you use a ’.’ to separate the text and the variables. Generally I recommend you use single quotes and the second option to make something like this:
$variable = 'text here'.$other_variable.'more text here';
...as it is faster and cuts down the need for “escaping” double quotes in html. Note that in the previous example the variable ‘variable’ is assigned a value of ‘text here’ followed by the value of variable ‘other_variable’ followed by ‘more text here’. So if the variable ‘other_variable’ had a value of ’-’ then variable ‘variable’ would have a value of ‘text here-more text here’. If you needed to have a literal single quote inside single quotes then you simply escape it with a backslash (\) so:
$text = 'I don\'t like to escape';
...is valid and makes variable ‘test’ have a value of “I don’t like to escape”, as we wanted.
Now What Have I Learnt?
Well, you can now assign values to variables, place variables into calculations, place text and variables together and escape, something that will crop up later. Good job!

