Last updated
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.
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.
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.";
}
?>
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.
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Using HashiCorp Vault with LDAP
How to use HashiCorp Vault to setup an LDAP backed secret store with read-only access for users in groups and read-write access for specific users
Linux and Unix xargs command tutorial with examples
Tutorial on using xargs, a UNIX and Linux command for building and executing command lines from standard input. Examples of cutting by character, byte position, cutting based on delimiter and how to modify the output delimiter.
Copy a file in Go
How to copy a file in Go. The ioutil package does not offer a shorthand way of copying a file. Instead the os package should be used.