You are currently viewing PHP Conditional Statements

PHP Conditional Statements

As a programmer, you often find situations to perform something based on certain type of conditions. PHP Conditional statements are the techniques we need to know to handle these situations.

These are the 4 conditional statements in PHP.

  • The if Statement
  • The if else Statement
  • The if elseif else Statement
  • The switch Statement

PHP – if Statement

We use if statement to execute a block of PHP code if one condition is true.

Syntax

if (condition A) {
    The PHP code here will execute only if the above condition A is true
}

Here is the sample program to demonstrate PHP if statement.

<?php
$first_number = 5;
$second_number = 10;
if ($first_number<$second_number){
echo "he..he..Condition is true";
}
?>

PHP – if else Statement

If else statement is used to execute a block of PHP code if one condition is true. But it will be extended to another level. That means if the condition is false it will execute another block of PHP code.

Syntax

if (condition A) {
    Block of PHP code here will execute only if the above condition A is true
} else {
    If the above condition A is false, the block of PHP code here will execute
}

Here is the sample program to demonstrate PHP if else statement.

<?php
$first_number = 10;
$second_number = 5;
if ($first_number<$second_number){
echo "he...he..this is true";
}else {
echo "he..he..this is false";
}
?>

PHP – if elseif else Statement

if elseif else statement can use to deal with two or more conditions. Normally, we use it to execute different blocks of PHP code for more conditions.

Syntax

if (condition A) {
    Block of PHP code here will execute only if the above condition A is true
} elseif (condition B) {
    Block of PHP code here will execute only if the above condition B is true
} else {
    Code to be executed if all A and B conditions are false;
}

Here is the sample program to demonstrate PHP if elseif else statement.

<?php
$name = 'perera' ;
if ($name == "Shehan" ) {
echo "Hiii Shehan Have a Nice Day!";
} elseif ($name == "Rasanga") {
echo "Hiii Rasanga Have a Nice Day!";
} else {
echo "You must be someone else";
}
?>

PHP – Switch Statement

The switch statement is used to compare a single variable with different conditions. It’s like a series of if statements. But when we need to check a list of numbers or strings switch statement is more reliable.

Syntax

switch (A) {
    case A1:
        PHP Code here will execute if A = A1;
        break;
    case A2:
        PHP Code here will execute if A = A2;
        break;
    case A3:
        PHP Code here will execute if A = A3;
        break;
    ...
    default:
        PHP Code here will execute if A is not equal to A1, A2, A3;
}

Here is the sample program to demonstrate the PHP switch statement.

<?php
 
$name = "Mark";
  switch($name)
  {
    case "Shehan":
               echo "hi....Shehan";
               break;
    case "Rasanga":
               echo "hi....Rasanga";
               break;
    default:
               echo "You must be someone else";
               }
?>

Leave a Reply