The if, if…else and if…elseif…else constructs are important features of any programming language.
The conditional statements provides us different actions for the different conditions. When we write code, we perform different actions for different decisions.
Like any other language, PHP is built out of a series of control statements. The control statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing or an empty statement.
So here in this tutorial, we will explain how to use control statements in PHP with example.
Also, read:
- 15+ regular expressions for PHP developers
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
In PHP we have the following conditional statements:
if statement – We use this control statement to execute some code only if a specified condition is true.
if…else statement – We use this control statement to execute some code if a condition is true and another code if the condition is false.
if…elseif….else statement – We use this control statement to select one of several blocks of code to be executed
switch statement – We use this control statement to select one of many blocks of code to be executed
1. The if Statement
Use the if statement to execute some code only if a specified condition is true.
The expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE – it’ll ignore it
Syntax
if (condition) { code to be executed if condition is true; }
The following example would display ” A is bigger than B” if $a is bigger than $b:
<?php if ($a > $b) echo "A is bigger than B"; ?>
2. The if…else Statement
elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE.
if (condition) code to be executed if condition is true; else code to be executed if condition is false;
For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:
<?php if ($a > $b) { echo "a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; } else { echo "a is smaller than b"; } ?>
3. The if…elseif….else Statement
Use the if….elseif…else statement to select one of several blocks of code to be executed.
if (condition) code to be executed if condition is true; elseif (condition) code to be executed if condition is true; else code to be executed if condition is false;
Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.
4. The Switch Statement
The switch statement is similar to IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.
switch ( ) { case condition1 break; case condition2 break; }
For example, the following code would display $i matched value as 0 or 1 or 2:
<?php switch ($i) { case 0: echo "i equals 0"; case 1: echo "i equals 1"; case 2: echo "i equals 2"; } ?>
You may also like:
- Library Management System with PHP & MySQL
- Online Food Ordering System with PHP & MySQL
- Build Hotel Reservation System with PHP & MySQL
- Working with php.ini file Configuration
- Control Statements in PHP
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Using Prepared Statement with PHP & MySQL
- How to Upload File in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
- Manipulating PHP arrays: push, pop, shift, unshift
- Remove Repeated Words From String in PHP
- Converting a PHP Array to a Query String
- 15+ regular expressions for PHP developers
- 10 Most Important Directory Functions in PHP
- 10 little known but useful PHP functions
- PHP Script to Download Large Files Reliably