Dynamic websites with PHP (part 4)

How do variables work in PHP?

Using variables in PHP is very simple. We don't have to worry about data types or anything like that because PHP is smart enough to deal with all that stuff behind the scenes.

Just remind me what a variable is

You can think of a variable as being a named box into which we can place (assign) just about any kind of data like a number or a line of text (string). Variables can also be used to store complex data like arrays, but we'll keep things simple for now.

Do PHP variables have special names?

Yes, PHP variables always begin with the dollar sign ($), for example $name. Other than that, names can contain any combination of letters, numbers and the underscore character. The underscore character is often used as a separator like $current_date.

There's one other thing about variable names you should be aware of; they are case sensitive, so $name and $Name are different variables. It's best practice to name all your variables using lower-case characters to avoid confusion.

OK, so how do we declare a variable and assign a value to it?

In PHP, declaring a variable and assigning a value to it is all part of the same process and can be done with a single statement:

$my_name = "David";

The statement above declares a variable called $my_name and uses the equals sign (the assignment operator) to give it a value; in this case a bit of text, referred to as a string.

Just like all statements in PHP, it is ended with the semi-colon character.

OK, show me some more variables being assigned

The following script assigns 3 variables, a string, a number and a value returned by a function. Pay special attention to the syntax in each case:

$building = "Seattle Space Needle";
$height = 184;
$today = date("l");

Notice that string values are quoted but numbers are not. Notice also that you don't have to assign an explicit value; you can assign a value returned by a function. In the example above, the date function is used to generate the day name (e.g. “Monday”) and that is the value assigned to $today.

Wow, that's really cool

Yes, it is and it's the ability to store dynamically generated values in variables that give scripting languages like PHP their power.

What can I do with variables once I've got them?

Well, you can do many things; you can compare them, use them to make decisions, and get them to interact with each other in various ways but one of the most basic things you need to know is how to print them:

<?php echo $today; ?>

As you can see, printing variables is easy, we just use the echo function that we met earlier, and pass it the name of our variable.

What if I want to add more text to that echo statement?

Typically, you might want to print something like “Today is Monday”. To do that, you would use the following statement:

<?php echo "Today is $today"; ?>

Ah, so the quotes are important?

Yes, the quotes are very important and they must be double quotes and not single quotes because variables are evaluated when placed between double quotes and not evaluated when placed between single quotes.

Could you just clarify that?

Yes, the following statement:

echo "Today is $today";

will print: Today is Monday

whereas this statement:

echo 'Today is $today';

will print: Today is $today

That's a bit confusing isn't it?

Maybe a little, but there is a good reason for these two methods of quoting strings. Single quotes are more efficient for PHP because there's no extra work involved, all strings are explicit. Whereas double quotes means that PHP has to look out for and evaluate any variables that happen to be in a string.

There's no rule about this, you need to use your own judgement when using quotes to decide which is best in any given situation.