Tuesday, April 20, 2010

Workshop 3 PHP Controls

In this workshop, we will look into Flow Control in PHP

If Statements
<?php 

$subject = "ITC382" ; 
If ($subject == "ITC382") { 
    Echo " I am in ITC382 class"; 
} 
?> 


If .. else statements

<?php 

$subject = "ITC254" ; 
If ($subject == "ITC382") { 
    Echo " I am in ITC382 class";}
Else {echo " I am in the wrong class";) }  
?>

If..elseif ..else statements

<?php 

$subject = "ITC254" ; 
If ($subject == "ITC382") { 
    Echo " I am in ITC382 class";}
Else if ($subject == "ITC254") { 
    echo "I am in ITC254 class ";} 
else { echo " I'm not too sure which class I should be in. Maybe I should be in $subject"; }  
?>


Switch statements
Switch is commonly used to execute different codes based on each expression used.

<?php 

$subject = "ITC382";

switch ($subject) { 
    
    case "ITC254": 
    echo "I am in ITC254 class";
    break;
    case "ITC382":
    echo "I am in ITC382 class";
    break; 
    default:
    print "I must have gotten lost again, I am suppose to be in $subject";
    break;
} 
?>


While statements
A continuos loop until a condition is reached.

<?php 

$count = 1; 

while ($count < 10) { 
    echo " This is line $count", "<br>";
    $count++; // Increment count by 1 
} 
?> 

Do..While statements
It is similar to the Do statement except that termination is at the end of the code.

<?php 

$count = 0; 

do { 
    echo " This is line $count", "<br>";
    $count++; // Increment count by 1 
} while ($count < 10);
?>

For statements
For statements are also similar to while statements, it is a more structured way to write looping statements with the initialization, testing and modification expression nested within the command.

<?php 
for ($count =1; $count <10; $count++) { 
 echo "This is line $count", "<br>";
}
?>

0 comments:

Post a Comment