HTML and PHP Resources

Short easy to follow tutorials & more…

205: Introduction to If-Else Conditionals in PHP

| 0 comments

Introduction

In programming conditionals are the constructs that control flow of a program. Conditional constructs allow a program to have logic that enables decision making.

Conditional construct in PHP include if, elseif, and else. The if and elseif are followed by one or more expressions that can be evaluated to a Boolean value. The else is not followed by a boolean expression, but simply by a block of code that will be executed in the case that the other Booleans expressions are false.

Boolean Expression

A Boolean expression is simply a set of one or more comparisons that result in a true or false value.

Comparison Operators

Comparison Operators are simply operators that compare two values. You can compare a variable to a value or two variables to each other. The operators are used in between the two values we are comparing and return a Boolean value, a true or false.

Operator Description Example Boolean Value of Example
== Checks for equality “abc” == “abc” true
!= Checks for unequality “abc” != “abc” false
> Greater than 1 > 2 false
< Less than 1 < 2 true
>= Greater than or equal 1 >= 2 false
<= Less than or equal 1 <= 2 true
=== Identity -
Checks value and data type
0 === false false
(NOTE: “0 == false” results in true)
!== Not an Identity -
Checks value and data type
0 !== false true
(NOTE: “0 != false” results in false)

example_comparison_operators.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
$string1 = "abc";
$string2 = "def";
$int1 = 100;
$float1 = 100.0;
// Checks for Equality
var_dump($string1 == $string2); // false
// Checks for Inequality
var_dump($string1 != $string2); // true
// Left value greater than right value?
var_dump($int1 > 100); // False
// Left value is less than right value?
var_dump($int1 < 100); // False
// Left value greater than OR equal to right value?
var_dump($int1 >= 100); // True
// Left value is less than OR equal to right value?
var_dump($int1 <= 100); // True
// Check for identity, check value and type
var_dump($int1 === $float1); // False
// Check for NOT identity, check value and type
var_dump($int1 !== $float1); // True

Joining Multiple Boolean Expressions

Its possible to have multiple Boolean expressions in order to match a set of conditions. In such a case the AND and OR key words or && for AND and || for OR symbols can be used to join multiple expressions. AND returns true if the expressions being joined are both true. In contrast  OR returns a true value when at least one of the expressions is true. It should be noted also that you can use any combination of AND and OR and group pairs of expressions with parenthesis  to meet more specific criteria in your scripts.

example_and_or.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$var1 = 100;
$var2 = "abc";
// Using AND
var_dump($var1 == 100 AND $var2 == "abc"); // True
var_dump($var1 == 200 AND $var2 == "abc"); // False
// Using OR
var_dump($var1 == 100 OR $var2 == "abc"); // True
var_dump($var1 == 200 OR $var2 == "abc"); // True
// Using AND and OR
var_dump(($var1 == 100 OR $var2 == "def") AND ($var1 == 200 OR $var2 == "abc")); // True
var_dump(($var1 == 100 AND $var2 == "abc") OR ($var1 == 200 AND $var2 == "abc")); // True

Syntax

The “if” Statement

The if statement should be followed by a Boolean expression encased in parenthesis in order to be parse correctly. After the parenthesis you can define a block of code to execute by wrapping any number of lines between curly brackets. You can omit the curly brackets if you only have a single line of code after the if statement. In the case that you have multiple lines of code that you want to execute but omit the curly brackets, only the first line is considered part of the if.

example_syntax_if.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$var1 = "cool";
if ($var1 == "Cool") {
// FALSE - Comparison is case sensative
// This will not execute since the expression is false
echo "Case InSensative?";
}
if ($var1 == "cool") {
// TRUE - The expression is a match in value.
echo "CASE SENSATIVE!";
}

The “elseif” and “else if” Statements

The “elseif” and “else if” statements work in a similar fashion to the “if”, they are followed by a Boolean expression encased in parenthesis and a line or a block of code that will get executed.

The only time an else if” statement gets executed is when the condition inside the “if” statement was false.

Note that you are able to have multiple “else if statements following an if. The parser will check each of the statements sequential until a true statement is found.

example_if_elseif.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$var1 = "cool";
if ($var1 == "Cool") {
// FALSE - Comparison is case sensative
// Parser moves to next "if" in the sequence
echo "Case InSensative?";
} else if ($var1 == "not-cool") {
// FALSE - Comparison is not a match
// Parser moves to next "if" in the sequence
echo "Not A Match";
} else if ($var1 == "cool") {
// TRUE - Comparison is a match
echo "Finally A Match";
}

The “else” Statement

The “else” statement is the final step in a conditional statement. This is an optional component of the conditionals as it functions as a fallback option in the case that none of the other options match. The else does not have a Boolean expression, but it does need a line or block of code that it should execute in the case none of the “if” or “else if” statements matched. If you do not have any code for the else, you can omit it entirely like the examples above.

example_if_elseif_else.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$var1 = "cool";
if ($var1 == "Cool") {
// FALSE - Comparison is case sensative
// Parser moves to "else if" in the sequence
echo "Case InSensative?";
} else if ($var1 == "not-cool") {
// FALSE - Comparison is not a match
// Parser moves to next "else if" in the sequence
echo "Not A Match";
} else if ($var1 == "warm") {
// FALSE - Comparison is not a match
// Parser moves to "else" in the sequence
echo "Not A Match Again";
} else {
echo "Not Match was Found, so I echo this!";
}

Summary

In summary the if-else constructs in PHP allow us to control flow of the script in order to execute specific piece of code depending on the value of variables we specify. You can use the “if” construct in conjunction with “elseif” and “else” or use it independently. Note that the expression following the “if” or “else-if” is always evaluated to a Boolean value.

Questions or Concerns?

If you happen to have any questions, concerns or corrections please let us know in the comment area or you can reach us at php@htmlandphp.com

Leave a Reply

Required fields are marked *.

*