PHP Control Statements
PHP Control Statements
Web Programming
Web Programming
if statement
The if construct is one of the most im"ortant feature of many languages% including PHP( )t allows for conditional e*ecution of code fragments( PHP features an if structure that is similar to that of c
Syntax: if (expression/condition){ statement; }
Web Programming
Example
<html> <head><title>Control Statements</title></head> <body> <?php $a !; $b "; if($a > $b) echo #a is bi$$er than b%; ?> </body> </html>
Information Technology Department
Web Programming
Web Programming
What is "b#
& <?php $a !; $b "; if($a < $b) { echo #a is bi$$er than b%; $b $a; } echo $b; ?> &
Web Programming
What is "b#
& <?php $a !; $b "; if($a < $b) echo #a is bi$$er than b%; $b $a; echo $b; ?> &
Web Programming
What is "b#
& <?php $a !; $b "; if($a > $b) echo #a is bi$$er than b%; $b $a; echo $b; ?> &
Web Programming
else
else e*tends an if statement to e*ecute a statement in case the e*"ression in the if statement evaluates to '()S*(
& $a $b
!; "; if($a < $b) { echo #a is bi$$er than b%; } else { echo #b is smaller than a%;
}
FEU EAST ASIA COLLEGE
Web Programming
else if $ elseif
+s its name suggests% is a combination of if and else
& $a $b
!; "; if($a > $b) { echo #a is bi$$er than b%; } elseif($a < $b) { echo #a is smaller than b%; } else { echo #a and b are e+,al%;
Web Programming
% tric&' one
<?php $a -; $b !; if($a $b) { echo #.he expression is ./0*%; } else { echo #.he expression is '()S*%; } ?>
FEU EAST ASIA COLLEGE
Web Programming
<?php $i -;
Web Programming
if ($i
?> <h2>.he condition is tr,e</h2> <center><b>$i is -</b></center> <?php } else { ?> <h2>.he condition is false</h2> <center><b>$i is not -</b></center> <?php } ?>
Information Technology Department
Web Programming
switch statement
The switch statement controls "rogram flow by e*ecuting a s"ecific set of statements% de"ending on the value of an e*"ression( )t com"ares the value of an e*"ression to a value contained within a s"ecial statement called a case label( + case label in a switch statement re"resents a s"ecific value and contains one or more statements that e*ecute if the value of the case label matches the value of the switch statement/s e*"ression(
Information Technology Department
Web Programming
switch statement
Synta*
s3itch (expression) { case label1 statement (s); brea4; case label1 statement(s); brea4; & defa,lt1 statement(s); }
Web Programming
Example
<?php $n,m 2; s3itch ($n,m) { case -1 echo #.he 5$n,m is 6ne7; brea4; case 21 echo #.he 5$n,m is .3o7; brea4; case "1 echo #.he 5$n,m is .hree7; brea4; defa,lt1 echo 7(ny 8,mber7; } ?>
Information Technology Department
Web Programming
Example
<?php $n,m !; s3itch ($n,m 9 2) { case -1 echo #.he 5$n,m is 6ne7; brea4; case 21 echo #.he 5$n,m is .3o7; brea4; case "1 echo #.he 5$n,m is .hree7; brea4; defa,lt1 echo 7(ny 8,mber7; } ?>
Information Technology Department
Web Programming
case Labels
case $*xample:ar1 statement(s); case #text strin$%1 statement(s); // ;ariable name // strin$ literal
case <!1 // inte$er literal statement(s); case =2<">?1 statement(s); // floatin$=point literal // conditional expression or operations
Web Programming
Repeating Code
while Statements
)t is one of the sim"lest ty"es of loo"s% which re"eats a statement or series of statements as long as a given conditional e*"ression evaluates to true( Synta*.
Web Programming
Example
$Co,nt -; 2 3hile ($Co,nt < !) { 3 echo #$Co,nt <br />%; 4 AA$Co,nt; 5 } End of loop. echo #<p>*nd of loop></p>%;
Information Technology Department
Web Programming
Example
$Co,nt -@; 3hile ($Co,nt > @) { echo #$Co,nt <br />%; ==$Co,nt; } echo #<p>Be ha;e liftoff></p>%;
Information Technology Department
Web Programming
Web Programming
%lternative S'ntax
<?php $i -; -@)1 echo $i; $iAA; end3hile; ?> 3hile ($i <
Web Programming
do*while Statements
The do>>>3hile statement will always e*ecute the bloc0 of code once% it will then chec0 the condition% and re"eat the loo" while the condition is true( Syntax:
Web Programming
Example
$Co,nt @; do { echo #.he co,nt is e+,al to $Co,nt<br/>%; AA$Co,nt; } 3hile ($Co,nt < 2);
Information Technology Department
Web Programming
for statement
The for loop is used when you 0now in advance how many times the scri"t should run(
Synta*. for (counter declaration and initialization; condition; update statement) { statement(s); }
Information Technology Department
Web Programming
for ($i @; $i < $co,nt; $iAA) { print(7<br>index ;al,e is 1 $i7); } one or more statements in the loo" body
12
Web Programming
Example
<?php for ($Co,nt @; $Co,nt<-@; AA$Co,nt) { echo $Co,nt; } ?>
Web Programming
brea&
brea0 ends e*ecution of the current for% foreach% while% do4while or switch structure(
<?php for ($i @; $i < -@; AA$i) { if ($i !) brea4; print 7$i<br>7; } ?>
0 1 2 3 4
Web Programming
Example
<?php for ($i - ; ; $iAA) { if ($i > -@) { brea4; } print $i; } ?>
Web Programming
Example
<?php $i -; for (;;) { if ($i > -@) { brea4; } print $i; $iAA; } ?>
Information Technology Department
Web Programming
continue
continue is used within loo"ing structures to s0i" the rest of the current loo" iteration and continue e*ecution at the condition evaluation and then the beginning of the ne*t iteration( <?php 0 for ($i @; $i < D; AA$i) { 1 if ($i ") 2 contin,e; 4 print 7$i<br>7; 5 } ?>
Information Technology Department
Web Programming
5rom this e*am"le% the print command was recogni3ed by the for function( )f ec o command will be used% the result will be error(
Web Programming
Laborator' %ctivit'
Create a PHP "rogram that will ma0e use of a day in a wee0 to change the bac0ground color of your web "age as well as its te*t color( 6ou may use all the control statements discussed in PHP(
Web Programming
Short .ui/ 0
1. What is $x, assuming that its initial value is 10 (3 pts) if($x>?) { if($x<E) $x @; } else $x -; 2. What is $x, assuming that its initial value is 10 (3 pts) if($x>?) { if ($x<E) $x @; else $x -; } 3. (4 pts ) $speed <!; $fee @>@; if ($speed > "!){ $fee 2@>@@; }elseif($speed > !@){ $fee ?@>@@; }elseif($speed > <!){ $fee D@>@@; } echo $fee;