PHP Tutorial 5 – if/else Conditional Statements and Comparison Operators

2 minute read

In programming, conditional statements are used to evaluate and compare sets of data. The “if” statement executes a block of code only if a certain condition is true. A comparison to this statement used in everyday life might be “If it is cloudy, then bring an umbrella” (cloudy being the condition, bringing an umbrella being the action or code). It is often used in conjunction with an “else” statement, which executes a block of code if the condition expressed in the if statement is not true.

<html>
<head>
<title>Conditional Statements</title>
</head>
<body>
 
< ?php

$number = 5;

if ($number == 5) {
	echo "The number is 5";
}

?>

</body>
</html>

The above code will output “The value is 5”. This is because the condition in line 11 is true; the value of the variable $number is equal to 5. The “==” is the comparison operator in this case, and translates to “is equal to”.

<html>
<head>
<title>Conditional Statements</title>
</head>
<body>
 
< ?php

$number = 2;

if ($number == 5) {
	echo "The number is 5";
} else {
	echo "The number is not 5";
}

?>

</body>
</html>

The above code will output “The number is not 5” because the condition expressed in line 11, “$number == 5” (the value of $number is equal to 5), is false. Because it is false, the code in the else statement is executed.

You may find that you want to test multiple conditions, and that making multiple if/else statements is inefficient. Thankfully, you can utilize “else if” statements to append additional blocks of code that you want to execute if other conditions are true.

<html>
<head>
<title>Conditional Statements</title>
</head>
<body>
 
< ?php

$number = 7;

if ($number == 5) {
	echo "The number is 5";
} else if ($number == 7) {
	echo "The number is 7";
} else {
	echo "The number is neither 5 nor 7";
}

?>

</body>
</html>

The above code will output “The number is 7” because the condition of the “else if” statement on line 13 is true. Note that if previous “if” or “else if” statements are true, further “else if” statements that are true will not be executed.

Comparison operators are the symbols in the “if” statement condition parentheses. So far, we’ve only used the “==” operator. However, there are several more operators that you can use.

Operator In words Example
< Is less than $X < 5
> Is greater than $X > 5
== Is equal to $X == 5
!= Is not equal to $X != 5
<= Is less than or equal to $X <= 5
>= Is greater than or equal to $X >= 5

Here is an example of the greater than operator in use:

<html>
<head>
<title>Comparison Operator</title>
</head>
<body>
 
< ?php

$number = 6;

if ($number > 3) {
	echo "The number is greater than 3";
}

?>

</body>
</html>

The above code will output “The number is greater than 3” because the value of the variable $number is greater than 3, and that is the condition that is being tested. If you set $number equal to a value less than or equal to 3, nothing will be output.

Leave a Comment