PHP Conditional Statements
This Article (Write a Conditional Statements Blog Post) is part of Oretes Academy Industry Certified Fullstack Developer in PHP Program.
Like any programming language, PHP supports conditional statements to execute specific blocks of code if a statement is true (or false) or some condition is met. If you’re familiar with conditional statements in other programming languages PHP should give you no trouble, but there are a couple lesser-known operators (ternary and null coalescing) that will be covered at the end.
Some PHP conditional statements are :-
if
statement - executes some code if one condition is trueif...else
statement - executes some code if a condition is true and another code if that condition is falseif...elseif...else
statement - executes different codes for more than two conditionsswitch
statement - selects one of many blocks of code to be executed
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) {
code to be executed if condition is true;
}
Example

Output
Have a good day!
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.
Syntax
if (condition) {
// if TRUE then execute this code
}
else{
// if FALSE then execute this code
}
Example

Output
Have a nice day!
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
}
Example

Output
Have a nice day!
Switch Statement
If you want to select one of many blocks of code to be executed, use the Switch statement.The switch statement is used to avoid long blocks of if..elseif..else code.
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
…
default:
code to be executed if n is different from all labels;
}
Example

Output
Today is Monday