PHP Conditional Statements

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 true
  • if...else statement - executes some code if a condition is true and another code if that condition is false
  • if...elseif...else statement - executes different codes for more than two conditions
  • switch 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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Managing Jupyter Notebooks and Conda Environments

How to Plan a Project: Learned Tips From a Terrible Project Planner

Fantastic Symbols and Where to Find Them — Part 1

What Is .NET Core?

HOW TO CHANGE MYSQL ROOT PASSWORD IN UBUNTU

A look at the open source Exposure Notification reference server

Exposure Notification Server Diagram

Base64 Encoding Algorithm

STEM and Full-Stack Web Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shraddha sonali Sharma

Shraddha sonali Sharma

More from Medium

Laravel Best Practices You Should Follow

7 Reasons Why PHP is so Famed For Web Development

Flutter 2.10: Latest Updates for Windows, iOS & Android

How to create XAMPP shortcut in Ubuntu’s Start Menu