PHP Conditional Statements

PHP Conditional Statements are used to do different actions.Different conditions can be perfomed using different statements.

PHP Conditional Statements

Generally,programmers write code  that perform different actions using different conditions.

PHP has below conditional Statements

     1.switch statement –used to select a block of code and executes it

  1. if…elseif….else statement-used to execute several different codes with atleast two conditions
  2. if…else statementthis statement executes code if a certain condition is false and other true

4 if statement –this statement is executed if the only one condition is true

 See also :Data Types in PHP 5

1.PHP switch Statement

Switch statement is used to do different tasks and consists of different conditions.

It selects one block of code and executes it leaving the others .

PHP switch Statement General Syntax

switch (r) {
    case 1:
        code that willl be executed if  r=1;
        break;
    case 2:
       code that willl be executed if  r=2;
        break;
    case 3:
code that willl be executed if  r=3;
        break;
    …
    default:
       code that will be executed no value is equal to r;

}

Note:

Break-is  used to prevent code running the next code.It will loop out once the condition has returned true.

Default-this statement will be executed if no match has been found.

Example:


<?php
$color = "blue";

switch ($color) {
case "Purple":
echo "You chose color Purple!";
break;
case "blue":
echo " You chose color blue!";
break;
case “white":
echo " You chose color white!";
break;
default:
echo " You chose color neither Purple, blue, nor white!";
}
?>

2.PHP  if…elseif….else statement

if…elseif….else statement executes different codes  for atleast two conditions or more.

PHP  if…elseif….else statement Syntax

if (condition one) {
    code that will be executed if true is returned after the check of the condition;
} elseif (condition two) {

       code that will be executed if true is returned after the check of the condition;
} else {

        The code that will be executed if condition one and two all returned false;}

Example:Simple PHP Grading System

With Help of PHP Operators,below code can be used to grade marks

<?php
$marks = 70;

if ($marks>50 $$<60) {
echo "Your Grade is C!";
} elseif ($marks>60&&<70) {
echo "Your Grade is B!";
}

elseif ($marks>70&&<80) {
echo "Your Grade is A-!";
}

elseif ($marks>80) {
echo "Your Grade is A!";
}

else {
echo "Your grade is E!";
}
?>

  1. PHP if…else Statement

 

This statement is execute if one condition return false and other true

PHP if…else Statement Syntax

if (condition) {
    Code here is executed if condition returns  true;
} else {
   Code here is executed if condition returns  false;
}

Example:
<?php
$marks =100;

if ($marks==100) {
echo "Pass”;
} else {
echo "fail";
}
?>

 4.PHP  if Statement

This statement has one condition that is executed if a true value is returned

PHP  if Statement Syntax

if (condition) {
this code is executed if the value returned is true;

}

Exampe:
<?php
$marks= 100;

if ($marks==100) {
echo "Pass";
}

 See my Next Lesson:PHP  while Loops

 

About the Author: Eric K

My name is Eric.Am a software developer with skills in Php,Mysql, Java , Android,Node.js Above all, I have certification in Cisco Networking Associate (CCNA) and holds degree in Information Technology. Hire me: Email : kawi3462@gmail.com