PHP Conditional Statements

PHP Conditional Statements are found in every programming language, these allows the program to decide a particular path of execution based on a comparative conditions. In Conditional statement, a condition is tested and then according to the result of the testing, i.e. either TRUE or FALSE, an action is performed.

Some PHP conditional statements are :-

Statement Description
if statement Executes a block of code if the given condition is true.
if-else statement Executes a block of code if a condition is true or another block of code if that condition is false.
if elseif else statement Used to test more than one condition with different blocks of code.
switch statement Alternative for If-elseif statement.

PHP if Statement

The PHP If statement is used to test a condition and then based on the results, if the condition is TRUE then a particular block of code is executed otherwise if the result is FALSE then the block of code is ignored and nothing happens. You can test more than one condition by using 'AND' and 'OR' operators. Let's see an example to understand it properly, also look at the syntax and write it down:

Syntax
if (condition){
// if TRUE then execute this code
}

Example

<?php $num=25; if($num<100){ echo "$num is less than 100"; } ?>

Output

25 is less than 100

As you can see in the example above, the condition evaluates to be TRUE and then a line got printed. But when the condition evaluates to false, then the block of code got ignored and nothing happened.


PHP if... else Statement

The If statement only performs an action when the given condition is TRUE, but what if we also want to give an alternative block of code in case of a FALSE evaluation. For this we need if-else statement, It allows the programmer to define actions for both scenarios.

The if-else statement provide two blocks of code, one under 'if' and the other under 'else'. If the condition evaluates to TRUE then the 'if' part will get executed otherwise the 'else' part will be executed.

Look at the syntax and then the example:

Syntax
if (condition) {
// if TRUE then execute this code
}
else{
// if FALSE then execute this code
}

Example

<?php $num=50; if ($num%2==0){ echo "$num is even number"; } else{ echo "$num is odd number"; } ?>

Output

50 is even number

PHP if…elseif…else Statement

Till now we were performing actions based on a given condition, but what if we want to test multiple conditions and want to define a block of code for each condition? We will use the if-elseIf Statement. We use this when we have multiple conditions of TRUE cases. It sounds a bit complex but it is not, once you get familiar with the syntax, you will understand it quickly:

Syntax
if (condition) {
// if TRUE then execute this code
}
elseif(condition) {
// if TRUE then execute this code
}
elseif(condition) {
// if TRUE then execute this code
}
else {
// if FALSE then execute this code
}

In this scenario all the condition will be checked one by one and if any one of them evaluates to TRUE then the block of code for that condition will be executed and all other blocks will be ignored but if all the conditions evaluates to FALSE then the 'else' statement's block will be executed. Look at the example below and try to understand it step by step.

Example

< ?php $t = date("H"); echo "<p>The hour (of the server) is " . $t; echo ", and will give the following message:</p>"; if ($t < "10") { echo "Have a good morning!"; } elseif ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>

Output

The hour (of the server) is 12, and will give the following message:
Have a good day!

Ternary Operators

The Trenary operators are a form of Shorthand Technique for the If-else statement. It uses a question mark (?) and a colon (:) with three operands: a condition which will be evaluated, an action for TRUE and an action for FALSE. Look at the syntax and things will get cleared

Syntax
(condition) ? if TRUE execute this : otherwise execute this;

Look at the example below in which it is shown how you can replace the if-else statement with the Ternary Operators:

Example

<?php $x = -12; if ($x > 0) { echo "The number is positive \n"; } else { echo "The number is negative \n"; } // This whole lot can be written in a // single line using ternary operator echo ($x > 0) ? 'The number is positive' : 'The number is negative'; ?>

Output

The number is negative
The number is negative




Color Picker

List of color names supported by all browser »



Character Code

List of character codes to display special character »



Language Code

List of all language supported by all browser »



Country Code

List of Country code supported by all browser »

Latest Updates

PHP Introduction

How to Run a PHP program in Xampp?

How can I create a phpinfo.php page?

How to Define a Variable in PHP

PHP Echo and Print Statements

Working with PHP Operators

Understanding the PHP Data Types

How to Write Comments in PHP

How to Define a Constant in PHP

PHP if...else...elseif Statements