Some Practise with PHP If Statements

If the user selected "church", then display the church image. If the user selected "kitten", then display another image. Here's some code:

<?PHP
$kitten_image = 1;
$church_image = 0;
if ($kitten_image == 1) {
print ("<IMG SRC =images/kitten.jpg>");
}
?>

Type that out, and save it as testImages.php. (Notice how there's no HTML!)

When you run the script, the kitten image should display. Let's look at the code and see what's happening.

The first two lines just set up some variables:

$kitten_image = 1;
$church_image = 0;

A value of 1 has been assigned to the variable called $kitten_image. A value of 0 has been assigned to the variable called $church_image. Then we have our if statement. Here it is without the print statement:

if ($kitten_image == 1) {
}

Notice how there's no semi-colon at the end of the first line - you don't need one. After the word "if" we have a round bracket. Then comes our variable name: $kitten_image. We want to test what's inside of this variable. Specifically, we want to test if it has a value of 1. So we need the double equals sign (==). The double equals sign doesn’t really mean “equals”. It means “has a value of”.

What we want to say is:

"If the variable called $kitten_image has a value of 1 then execute some code."

To complete the first line of the if statement we have another round bracket, and a left curly bracket. Miss any of these out, and you'll probably get the dreaded parse error!

The code we want to execute, though, is the print statement, so that our kitten image will display. This goes inside of the if statement:

if ($kitten_image == 1) {
print ("<IMG SRC =images/kitten.jpg>");
}

You need the semi-colon at the end of the print statement.

But if your if statement only runs to one line, you can just do this:

if ($kitten_image == 1) { print ("<IMG SRC = images/kitten.jpg>"); }

In other words, keep everything on one line. PHP doesn't care about your spaces, so it's perfectly acceptable code. Not very readable, but acceptable!
To make use of the church image, here's some new code to try:

<?PHP
$kitten_image = 0;
$church_image = 1;
if ($kitten_image == 1) {
print ("<IMG SRC =images/kitten.jpg>");
}
if ($church_image == 1) {
print ("<IMG SRC =images/church.jpg>");
}
?>

Notice that the $kitten_image variable now has a value of 0 and that $church_image is 1. The new if statement is just the same as the first. When you run the script, however, the church image will display. That's because of this line:

if ($kitten_image == 1) {

That says, "If the variable called $kitten_image has a value of 1 ... ". PHP doesn't bother reading the rest of the if statement, because $kitten_image has a value of 0. It will jump down to our second if statement and test that:

if ($church_image == 1) {

Since the variable called $church_image does indeed have a value of 1, then the code inside of the if statement gets executed. That code prints out the HTML for the church image:

print ("<IMG SRC =images/church.jpg>");

we can use an if ... else statement. Like this:

<?PHP
$kitten_image = 0;
$church_image = 1;
if ($kitten_image == 1) {
print ("<IMG SRC =images/kitten.jpg>");
}
else {
print ("<IMG SRC =images/church.jpg>");
}
?>

Copy this new script, save your work, and try it out. You should find that the church image displays in the browser. This time, an if … else statement is being used. Let’s see how it works.

The syntax for the if else statement is this:

if (condition_to_test) {
}
else {
}

If you look at it closely, you’ll see that you have a normal If Statement first, followed by an “else” part after it. Here’s the “else” part:

else {
}

Again, the left and right curly brackets are used. In between the curly brackets, you type the code you want to execute. In our code, we set up two variables:

$kitten_image = 0;
$church_image = 1;

The variable called $kitten_image has been assigned a value of 0, and the variable called$church_image has been assigned a value of 1. The first line of the if statement tests to see what is inside of the variable called $kitten_image. It’s testing to see whether this variable has a value of 1.

if ($kitten_image == 1) {

What we’re asking is: “Is it true that $kitten_image holds a value of 1?” The variable $kitten_image holds a value of 0, so PHP sees this as not true. Because a value of “not true” has been returned (false, if you like), PHP ignores the line of code for the if statement. Instead, it will execute the code for the “else” part. It doesn’t need to do any testing – else means “when all other options have been exhausted, run the code between the else curly brackets.“ For us, that was this:

else {
print ("<IMG SRC =images/church.jpg>");
}

So the church image gets displayed. Change your two variables from this:
$kitten_image = 0;
$church_image = 1;
To this:
$kitten_image = 1;
$church_image = 0;

Run your code again and watch what happens. You should see the kitten! But can you work out why?

You can also add “else if” parts to the If Statements. The syntax is this:

else if (another_condition_to_test) {
}

Change your code to this, to see how else if works:

<?PHP
$kitten_image = 1;
$church_image = 0;
if ($kitten_image == 1) {
print ("<IMG SRC =images/kitten.jpg>");
}
else if ($church_image == 1) {
print ("<IMG SRC =images/church.jpg>");
}
else {
print ("No value of 1 detected");
}
?>

Here’s we’re just testing to see which of our variables holds a value of 1. But notice the “else if” lines (and that there’s a space between else and if):

else if ($church_image == 1) {
print ("<IMG SRC =images/church.jpg>");
}

What you’re saying is “If the previous if statement isn’t true, then try this one.” PHP will then try to evaluate the new condition. If it’s true (the $church_image variable holds a value of 1), then the code between the new curly brackets gets executes. If it’s false (the $church_image variable does NOT holds a value of 1), then the line of code will be ignored, and PHP will move on.

To catch any other eventualities, we have an “else” part at the end. Notice that all parts (if, else if, and else) are neatly sectioned of with pairs of curly brackets:

if ($kitten_image == 1) {
}
else if ($church_image == 1) {
}
else {
}

You can add as many else if parts as you like, one for each condition that you want to test. But change your two variables from this:

$kitten_image = 1;
$church_image = 0;
to this:
$kitten_image = 0;
$church_image = 0;