Monday, July 15, 2013

Lesson 3 - Conditional Statements


Hello people, now we are going to talk about more basic stuff with PHP. Until now we know about setting up a server , how to print a statement , and assign variables and manipulate it. Today we are going to discuss about conditional statements. Most of the time conditional statements outputs Boolean value. (Either true or false). To accomplish this task we need a operand to complete the condition. Below mentioned are , common operands that we use in development.

Example
Name
Result
$a == $b
Equal
TRUE if $a is equal to $b after type juggling.
$a === $b
Identical
TRUE if $a is equal to $b, and they are of the same type.
$a != $b
Not equal
TRUE if $a is not equal to $b after type juggling.
$a <> $b
Not equal
TRUE if $a is not equal to $b after type juggling.
$a !== $b
Not identical
TRUE if $a is not equal to $b, or they are not of the same type.
$a < $b
Less than
TRUE if $a is strictly less than $b.
$a > $b
Greater than
TRUE if $a is strictly greater than $b.
$a <= $b
Less than or equal to
TRUE if $a is less than or equal to $b.
$a >= $b
Greater than or equal to
TRUE if $a is greater than or equal to $b.

So lets look at the conditional statements that we use to make our logic to work in real world.

IF...ELSE Statement

The structure of if..else statement goes as follows
if(condition){
   statement 1;
}elseif(condition){
   statement 2;
}else{
   statement 3;
}
If else statement work as same logic that we use in our everyday life. If our first condition is met true then we'll execute first statement. Else if we met the second condition true , then we execute the second statement. And if we don't met any of our conditions then third statement will be executed. Let's get this logic with real life scenario with Pseudo Code below.

/* I am hungry right now. I wonder around the city. */
if(I met a Burger King){
   Have a Burger;
}elseif(I met Pizza Hut){
   Have a Pizza;
}else{
   Have a Rice and Curry from Mathara Food Corner;
}
Now, let me show you PHP coding for above mentioned scenario.
/* I am hungry right now. I wonder around the city. */
if($shopImet == 'BurgerKing'){
   echo 'Have a Burger';
}elseif($shopImet == 'PizzaHut'){
   echo 'Have a Pizza';
}else{
   echo 'Have a Rice and Curry from Mathara Food Corner';
}
Also , if I wan't to have Pizza today and if I don't find Pizza shop have anything else scenario is converted to PHP like following
/* I am hungry right now. I wonder around the city. */
if($shopImet == 'PizzaHut'){
   echo 'Have a Pizza';
}else{
   echo 'Have a Rice and Curry from Mathara Food Corner';
}

IF.. ELSE short hand expression

As we getting progress on conditional statements , PHP gives us some shorthand if....else usage technique. The format is as follows.
 Condition ? Statement on True : Statement on false )
 Example:
echo ($result >=75 ? 'pass' : 'fail'); //if result is 85 this will print pass
echo ($result >= 75 ? ($age < 15 ? 'genius' : 'good'): 'fail'); // bit complicated ha! Naah this is easy if result is 80 and age is 10 then this will print genius. 

SWITCH  Statement

Now we know about If ....Else statements. In production we may need heavy nested  IF....ELSE statements. Experts suggested that using switch statements rather than nested  IF...ELSE statement is less complicated and highly readable. Let's checkout syntax of  
SWITCH(Expression){
  CASE scenario_1:
     statement_1;
  BREAK;
  CASE scenario_2:
     statement_2;
  BREAK;
  CASE scenario_3:
     statement_3;
  BREAK;
  DEFAULT:
     default_statement;
} 
When you pass an expression to switch statement it will check for cases that will match its cases. If there is a match that case will be executed. If there is no match for given cases and DEFAULT is defined then default statement will be executed. But there is a tweak. What happens when you do not mention BREAK statement like following.

SWITCH(Expression){
  CASE scenario_1:
     statement_1; 
  CASE scenario_2:
     statement_2; 
  CASE scenario_3:
     statement_3; 
  DEFAULT:
     default_statement;
}  // if all three scenarios matches then statement_1,statement_2,statement_3 will be executed


OR 

SWITCH(Expression){
  CASE scenario_1:      
  CASE scenario_2: 
  CASE scenario_3:
     statement_if_1_or_2_or_3_are_true; 
  DEFAULT:
     default_statement;
} 
 We will discuss more about Loops in our next lesson.

1 comment: