Home > Courses > Web development using PHP and MySQL > Control flow (Branching)

Control flow (Branching)

Subject: Web development using PHP and MySQL
Is use to control the flow of our program.

The programs we have written so far had executed sequentially, however logic demands that if our program is to be useful it must perform some task based on some conditions. For example if age is above 18, if score is greater than 70, if user is registered or gender is male (if, else if, else statement). OR execute a task over a period of time based on some conditions and patterns. For example generate even number between 1 and 2000 (for and while loop)

If, Else If, Else Statement
Conditional statement or branching are use when you want execute a block of code if a condition is true or false.

Example 1
IF Statement

<?php
//design a variable and assign value to it
$age=30;
//check if $age is greater than of equal to 18
if($age>=18){
    //if condition is true execute this block of code
    print "Welcome you are old enough to start";
}
?>


Sometime you may want to handle the result when the condition is false. In example 1, if the variable age is assigned 15 then the condition will be false and there will be no output, unless the ELSE Statement is introduced.

Example 2
ELSE Statement

<?php
$age=15;
if($age>=18){
    print "Welcome you are old enough to start";
}else{
   print "Sorry you are underage";
}
?>


Sometimes you may need to handle more than one condition, the you may use ELSE IF also called nested if else

Example 3
ELSE IF Statement

<?php
$score=50;
if($score>69){
    print "Distinction";
}else if($score<10){
    print "Fail";
}else{
   print "Pass";
}
?>

In example 3, the first condition check if the variable score is greater than 69 (if this first condition is true then "Distinction" is shown), the second condition check if the variable is less than 10 (if this condition is true then "Fail" is displayed), however if both first and second conditions are false then the code in the "else" block will execute. Note: it's only one out of the three blocks of code that must execute, based on the value of the variable score.

Switch Statement
Switch is used to execute one set of statement from multiple conditions. Use instead of the nested if else

It's syntax is as follows:

switch(expression) { 
case value1: 
     // code if the above value is matched    
     break; // optional 
case value2: 
     // code if the above value is matched
     break; // optional 
default: 
// code to be executed when all the above cases are not matched; 
} 


Example 4

<?php
$score=70;
switch(true){
case $score>69:
     print "Distinction";
     break; 
case $score<10:
     print "Fail";
     break; 
default: 
     print "Pass";
}
?>


Example 5

<?php
$friend = "bayo"; 
switch ($friend) { 
    case "chioma": 
        print "Your best friend is Chioma"; 
        break; 
    case "bayo": 
         print "Your best friend is Bayo"; 
         break; 
    case "ahmed": 
         print "Your best friend is Ahmed"; 
         break; 
     default: 
         print "I don't know your friend"; 
}
?>


By: Benjamin Onuorah

Comments

No Comment yet!

Login to comment or ask question on this topic


Previous Topic Next Topic

Supported by