PHP Conditional Statements

There are many statements available in PHP that you can use to make decisions :

The if Statement

The if statement is used to execute a block of code if condition is true then evaluates if block.

Syntax :

if (condition) {
  code to be executed if condition is true;
}

Example :

<?php
$t = date("H");

if ($t < "15") {
  echo "Have a good day!";
}
?>

The if else Statement

The if else statement first executes some code if a condition is true and another code if that condition is false.

Syntax :

if (condition) {
  code to be executed if condition is true;
} else {
  code to be executed if condition is false;
}

Example :

<?php
$a = 20;
$b = 10;

if ($a >= $b) {
  echo "A is big!";
} else {
  echo "B is big!";
}
?>

The if elseif else Statement

The if elseif else statement is used to combine multiple if else statements.

Syntax :

if (condition) {
  code to be executed if this condition is true;
} elseif (condition) {
  code to be executed if first condition is false and this condition is true;
} else {
  code to be executed if all conditions are false;
}

Example :

<?php
$a = 20;
$b = 10;

if ($a > $b) {
  echo "A is big!";
} else if($b > $a){
  echo "B is big!";
}else{
  echo "A and B equals!";
}
?>