Domesticated Brain

PHP Conditional Statements

In programming we often find situations to perform something based on certain type of conditions. Conditional statements are the techniques we need to know to handle the above situations.

In PHP there are 4 conditional statements to be taken under consideration

 

PHP - if Statement

In PHP programming we use if statement when we need to execute a block of PHP code if one conditions is true

Syntax

if (condition ) {

    The PHP code here will execute only if the above condition 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 extend to another level. That means if the condition is false it will allow us to execute a another piece 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

In PHP programming if elseif else statement can use to deal with two or more conditions. Normally we use the if elseif else statement when we need 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 basically 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, A;

}

 

Here is the sample program to demonstrate PHP switch statement

<?php

 

$name = "Shehan";

  switch($name)

  {

    case "Shehan":

               echo "hi....Shehan";

               break;

               case "Rasanga":

               echo "hi....Rasanga";

               break;

               default:

               echo "You must be someone else";

               }

?>

 

If you have found  what you expected please comment and like on our facebook page