PHP 101 - The if statement

I get a fair few requests from designers asking for help with basic PHP. So I'm going to write a series on very basic PHP. It is not hard so let's start with a robot and the if statement.

The if robot

The if robot

The if statement is integral to programming languages. It allows you to test for something and then do something else. Using the if statement in PHP is easy and is not difficult to learn.

Imagine you are walking down a street in Edinburgh and you see a robot standing on the side of the street. The robot has one big red button on its chest.This is a classic example of a situation where an if statement should be used.

The code

Here's what an if statement looks like using the example of the if robot.

<?php
$button 
"pressed";

if (
$button == "pressed"{
    
echo "I am alive and will now perform Sex Machine by James Brown";
}
?> 

First of all we have a php tag. This allows PHP code to be run. Then we have the $button variable. A variable is just something that could be more than one thing. Variables allow you to hold a piece of information temporarily. PHP variables are identified by a dollar sign followed by the name. In this case it is the button on the robot. It can be pressed, unpressed or broken. If the button is pressed the robot will perform a song. The if statement tests whether the statement within the brackets is true. The == operator tests whether the two options are equivalent. In this case they are and we are using the echo statement to write the status to the page. Finally we have our closing PHP tag.

Testing different conditions

For our example we have used an if statement to do something if the button is pressed. But there are two further options available to test for different conditions - elseif and else. Using elseif we can test for something else and we can then use else to pick up anything that didn't match any of our conditions. Let's say we want to test if the button is broken or if nothing has happened. We can do this:

<?php
$button 
"pressed";
if (
$button == "pressed"{
    
echo "I am alive and will now perform Sex Machine by James Brown";
}
elseif ($button == "broken"{
    
echo "Malfunction! Broken, destroyed, smashed.";
}
else {
    
echo "Nobody wants to press my button. I am so alone.";
}

?> 

Shorter syntax

When you are starting out I recommend that you use the syntax above as it is easier to read and understand. Once you are comfortable though if you are just testing for two conditions (true and false would be a good example) you can use a shorter way of writing an if statement. Here's the standard way of finding out if someone is hungry:

<?php
$hungry 
true;
if(
$hungry{
    
echo "Feed me now! I need food!";
else {
    
echo "Thank you my good man but I am not hungry";
}
?> 

This can be also be written like this:

<?php
$hungry 
true;
echo 
$hungry "Feed me now! I need food!" "Thank you my good man but I am not hungry";
?> 

The alternative syntax is more difficult to read but much shorter.

Links

Credits

Comments

Derek Allard
Oct 28 2008

Absolutely love the robot references and c’mon… Sex Machine!?!  This article made my day.  Respectfully linked.

Jared
Oct 29 2008

NOOOOOOOOOOOOOOOOOOOO!

You FOOL, you will only CONFUSE the poor HUMAN with that TERNARY nonsense!

ESCAPE! ESCAPE! ESCAPE!

alex
Oct 29 2008

The ternary operator is useful.. but often I have to redo it to { } code.

I only use them when I’m 90% sure I won’t have to add additional code to the statement.

This article sounds like a good starting point for people wanting to learn PHP.

kevinseo
Oct 31 2008

I’m using php6…is quite different with php5

alex
Oct 31 2008

php 6? is that out?

where have I been?

John
Dec 25 2008

Good for beginners I suppose…

Have your say

Post details

28th October 2008

6 Comments

What's this?

This is a journal entry written by George Ornbo, a web designer who lives and works in London, England.

Subscribe

Journal articles by RSS

Journal articles by Email

Recommended Links by RSS

Participating / Attending

Inclusive New Media Design

dConstruct 2008 - Designing the Social Web

Member of the 9 Rules Network Media Temple