Tutorials Books Videos Forums

Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

If/Else Statements

by kirupa   | filed under Web, HTML, CSS, and XML

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

If/else statements are very useful because they allow your code to be expressed in certain situations but not in others. You can control when those situations occur by using control statements such as if/else.

Before we delve any further, a basic If / Else statement in PHP looks like the following:

<?php
if (true) {
  do something
  } else {
  do something else
}
?>

Simply put, if a condition happens to be true, the code in pink will be executed. If the condition happens to be false, the code in orange, will be executed. The Else statement is optional though. If you do not specify an Else statement, your code will not do anything if the condition happens to be false.

For example, here is the code without an Else statement:

<?php
if (true) {
  do something
}
?>

Let's take an example involving a robot called kirupaTron that is programmed to do among many other things, cross a busy intersection.

kirupaTron is at an intersection. He is programmed to wait for the Walk light before crossing the intersection. Once kirupaTron sees the Walk light come on, he knows it must be safe to cross the intersection. The logic behind his action is controlled by an If / Else statement.

The robot checks to see IF the Walk light turns on. If the Walk light is on, the robot decides to walk. IF the walk light is off, the robot decides to stay put. Lucky for us, kirupaTron is programmed in PHP, and the code that helps him at intersection looks like the following:

<?php
if ($light == "Walk") {
  print("kirupaTron - start walking.");
  } else {
  print("kirupaTron - don't walk.");
}
?>

The above code is a simplified form that is similar to what is displayed above. If the variable, $light is set to Walk, the condition becomes true, and therefore, the code will execute. If the $light is set to something other than Walk, the condition fails, and the code for not walking will execute.

For example, copy and paste the following code into a new PHP document:

<?php
$light = "Stop";
if ($light == "Walk") {
  print("kirupaTron - start walking.");
  } else {
  print("kirupaTron - don't walk.");
}
?>

When you preview the page containing the above code, you will see that the text for not walking is displayed. But why? The condition for the if/else statement to be true is when $light equals "Walk". Since in the previous line, we mention that $light equals "Stop", the condition fails. Therefore, the code for not walking executes.

Now, let's say you want to make the condition true. Well, in that case, replace the "Stop" with "Walk." Your first line of code should look like the following:

$light = "Walk";

Now, test your page again. You will find that the code for for  walking is displayed. The condition is true. The reason the condition is true is because the variable $light does equal "Walk."

In the next section we will cover more about if and else statements such as conditional and logical operators.



In the previous section we covered the basic structure of if and else statements. This page will cover in greater detail various modifications for the if and else statements.

Conditional Operators

All of this time I have been using conditional operators such as == to determine whether a condition is true or not. But I never elaborated on what the various conditional operators are, and how they can be used to see if something is true or not. I think now is a good time to explain that.

The following table lists the more popular conditional operators you can use in PHP:

Operator Description
== Equals
>= Greater than or Equal
<= Less than or Equal
> Greater than
< Less than
!= Not Equal

An example using something other than the equals conditional operator could be:

<?php
$num = 5;
if ($num > 4) {
  print("Number greater than 4.");
}
?>

In the preceding example we used the greater than ( > ) operator that compares whether the variable $num is greater than 4. Since 5 is greater than 4, the statement is true, and the code will be executed.

If you were to replace the greater than operator ( > ) in the above code with the not equal  ( != )operator, the code will be true also. 5 does not equal 4, and that is true!


Logical Operators

Throughout this tutorial, our If statements depended on only one condition. If that one condition was true, the code would executed. Let's introduce logical operators that will allow you to set multiple conditions.

The operators that you will use frequently is AND and OR.

 
Operator Description
&& And
|| Or
and And
or Or

Here is a PHP example using one of the logical operators:

<?php
$username = admin;
$password = password;
if (($username == admin) && ($password == password)) {
  print("Access granted!");
}
?>

The first condition checks to make sure that $username equals admin. The second condition checks to make sure that $password equals password. The logical operator && checks to see if both condition statements are true.

If your value for password changed, the condition would fail. Even though one condition is true, because the && logical operator needs BOTH conditions to be true, the test fails. If you used the || operator, all you would need is for one condition to be true. Needless to say, if both conditions fail, && and || will not help make your statements true.

I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
Cheers!
Kirupa Chinnathambi
kirupaBlog


Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--