The conditional statement is used to perform different actions based on different conditions.

I. What is the conditional statement in PHP?

A conditional statement is a statement used to change the flow of a program based on a certain condition.

Usually when you write code, you want to perform different actions for different conditions, then you can use conditional statements to do this.

In PHP, we have the following conditional statements:

  • if statement - executes some code if a condition is true.
  • if... else statement - executes the code if a condition is true and another code if that condition is false.
  • if... elseif... else statement - executes different code for more than 2 conditions.
  • switch statement - selects one of many code blocks to be executed.

II. IF Statement

The if statement will execute a code if a condition is true.

1. Syntax

if (condition) {
 // A piece of code is executed if the condition is true
}

2. Example

We have an example as below: check if number a is even or odd.

$x = 12;
$y = $x % 2; // $y is the remainder. If the remainder is 0, then $x is an even number.
if ($y == 0){
     echo 'Number '.$x.' is an even number!';
}

III. IF ... ELSE Statement

The if... else statement executes a piece of code if a condition is true or another code if the condition is false.

1. Syntax

if (condition) {
// The statement is executed if the condition is true.
}else {
// The statement is executed if the condition is false.
}

2. Example

We have an example as below: check if number a is even or odd.

$x = 13;
$y = $x % 2; // $y is the remainder. If the remainder equals 0, then $x is an even number. Otherwise, it is odd.
if ($y == 0){
     echo 'Number '.$x.' is an even number!';
}else{
     echo 'Number '.$x.' is an odd number!';
{

IV. IF ... ELSE IF... ELSE Statement

The if... elseif... else statement will execute different pieces of code for more than 2 conditions.

1. Syntax

if (condition) {
// The statement is executed if the condition is true.
}else if(condition){
// The statement is executed if the condition is true.
}else{
// The statement is executed if both conditions above are false.
}

2. Example

We have an example as follows: check what color the following color code is.

$color = 'green';
if ($color=='red') {
 echo 'This is red color';
}else if($color=='blue'){
 echo 'This is blue color';
}else{
 echo 'This is green color';
}

V. SWITCH Statement

The switch statement is as meaningful as the if... elseif... else statement but is more concise. This statement is used to execute one of the conditions.

1. Syntax

Note: the break statement is used to prevent code from running through the next conditions.

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 does not match any cases above;
}

2. Example

We have an example as follows: check what color the following color code is.
 

$color = 'red';
switch ($color) {
  case 'blue':
    echo 'This is blue color';
    break;
  case 'yellow':
    echo 'This is yellow color';
    break;
  case 'black':
    echo 'This is black color';
    break;
  default:
    echo 'This is red color';
}

 

Ngọc Phương

Web Developer

Thank you for visiting my blog. My name is Phuong and I have more than 10 years of experience in website development. I am confident in asserting myself as an expert in creating impressive and effective websites. Anyone in need of website design can contact me via Zalo at 0935040740.

0 feedback