php control statements
TRANSCRIPT
-
8/13/2019 PHP Control Statements
1/35
Web Programming
FEU EAST ASIA COLLEGEInformationTechnologyDepartment
The PHP Control Statements
Conditional StructureRepetitive Structure
-
8/13/2019 PHP Control Statements
2/35
Web Programming
FEU EAST ASIA COLLEGEInformationTechnologyDepartment
Review on the Language Structure of
PHP
How to start PHP?
Creating and saving your PHP files
PHP Delimeters
Comments in PHP The use of Single and Double Quotations
Variables (scalar and Constant Values
Concatenation symbol !sca"e character and Casting
#"erators ($%$&% &&% ''
-
8/13/2019 PHP Control Statements
3/35
Web Programming
FEU EAST ASIA COLLEGEInformationTechnologyDepartment
if statement
The ifconstruct 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; }
-
8/13/2019 PHP Control Statements
4/35
Web Programming
FEU EAST ASIA COLLEGEInformationTechnologyDepartment
Example
Control Statements
-
8/13/2019 PHP Control Statements
5/35
Web Programming
FEU EAST ASIA COLLEGEInformationTechnologyDepartment
!roup Several Statement
&
-
8/13/2019 PHP Control Statements
6/35
Web Programming
FEU EAST ASIA COLLEGEInformationTechnologyDepartment
What is "b#
&
-
8/13/2019 PHP Control Statements
7/35
Web Programming
FEU EAST ASIA COLLEGEInformationTechnologyDepartment
What is "b#
&
-
8/13/2019 PHP Control Statements
8/35
Web Programming
FEU EAST ASIA COLLEGEInformationTechnologyDepartment
What is "b#
&
-
8/13/2019 PHP Control Statements
9/35
Web Programming
FEU EAST ASIA COLLEGEInformationTechnologyDepartment
else
elsee*tends an ifstatement to e*ecute astatement in case the e*"ression in theif
statement evaluates to 'S* &
$a !;
$b ";
if($a < $b) {
echo #a is bier than b%;
} else { echo #b is smaller than a%;
}
-
8/13/2019 PHP Control Statements
10/35
Web Programming
FEU EAST ASIA COLLEGEInformationTechnologyDepartment
else if $ elseif
+s its name suggests% is a combination of ifand else
&
$a !;
$b "; if($a > $b) {
echo #a is bier than b%;
} elseif($a < $b) {
echo #a is smaller than b%;
} else {
echo #a and b are e+,al%;
-
8/13/2019 PHP Control Statements
11/35
Web Programming
FEU EAST ASIA COLLEGE
InformationTechnologyDepartment
% tric&' one
-
8/13/2019 PHP Control Statements
12/35
Web Programming
FEU EAST ASIA COLLEGE
InformationTechnologyDepartment
%lternative S'ntax for if
)t is "ossible to write control statementsusing the following ,colon- format.
-
8/13/2019 PHP Control Statements
13/35
Web Programming
FEU EAST ASIA COLLEGE
InformationTechnologyDepartment
(ixing HT(L with PHP Control
Statements
.he condition is tr,e
$i is -
.he condition is false
$i is not -
-
8/13/2019 PHP Control Statements
14/35
Web Programming
FEU EAST ASIA COLLEGE
InformationTechnologyDepartment
switch statement
The switchstatement controls "rogram flow by e*ecutinga 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 valuecontained within a s"ecial statement called a case label
+ case label in a switch statement re"resents a s"ecificvalue 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
-
8/13/2019 PHP Control Statements
15/35
Web Programming
FEU EAST ASIA COLLEGE
InformationTechnologyDepartment
switch statement
Synta* s3itch (expression) {
case label1
statement (s);
brea4; case label1
statement(s);
brea4;
&
defa,lt1
statement(s);
}
-
8/13/2019 PHP Control Statements
16/35
Web Programming
FEU EAST ASIA COLLEGE
InformationTechnologyDepartment
Example
-
8/13/2019 PHP Control Statements
17/35
Web Programming
FEU EAST ASIA COLLEGE
InformationTechnologyDepartment
Example
-
8/13/2019 PHP Control Statements
18/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
case Labels
case $*xample:ar1 // ariable name
statement(s);
case #text strin%1 // strin literal
statement(s);
case !1 // inteer literal
statement(s);
case =2"1 // floatin=point literal
statement(s);
Case ($time < -@@)1 // conditional expression
statement(s); or operations
-
8/13/2019 PHP Control Statements
19/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
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 givenconditional e*"ression evaluates to true
Synta*.
3hile (conditional expression) {
statement(s);
}
-
8/13/2019 PHP Control Statements
20/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
Example
$Co,nt -;
3hile ($Co,nt
-
8/13/2019 PHP Control Statements
21/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
Example
$Co,nt -@; 3hile ($Co,nt > @) {
echo #$Co,nt
%;==$Co,nt; }
echo #
Be hae liftoff
%; -
8/13/2019 PHP Control Statements
22/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
)efine the output
$Co,nt -; 3hile ($Co,nt
-
8/13/2019 PHP Control Statements
23/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
%lternative S'ntax
-
8/13/2019 PHP Control Statements
24/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
do*while Statements
Syntax:
do {
statement(s);
} 3hile (conditional expression);
The do3hilestatement 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
-
8/13/2019 PHP Control Statements
25/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
Example
$Co,nt @;
do {
echo #.he co,nt is e+,al to
$Co,nt
%;AA$Co,nt;
} 3hile ($Co,nt
-
8/13/2019 PHP Control Statements
26/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
for statement
Synta*.
for(counter declaration and initialization;condition; update statement) {
statement(s);
}
The for loopis used when you 0now in advance how
many times the scri"t should run
-
8/13/2019 PHP Control Statements
27/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment 12
The for loop
+ counting loo" is usually im"lemented with for
for ($i@; $i < $co,nt; $iAA) {
print(7
index al,e is 1 $i7);}
initiali3e chec0 for limit
u"date loo" control inde*
shorthand for i+i,-
one or more statements in the loo" body
-
8/13/2019 PHP Control Statements
28/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
Example
-
8/13/2019 PHP Control Statements
29/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
brea&
brea0 ends e*ecution of the current for% foreach%while% do4while or switch structure
0123
4
-
8/13/2019 PHP Control Statements
30/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
Example
-
8/13/2019 PHP Control Statements
31/35
W b P i
-
8/13/2019 PHP Control Statements
32/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
continue
continue is used within loo"ing structures to s0i" the restof the current loo" iteration and continue e*ecution at thecondition evaluation and then the beginning of the ne*titeration
-
8/13/2019 PHP Control Statements
33/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
printinside forstatement
-
8/13/2019 PHP Control Statements
34/35
Web Programming
FEU EAST ASIA COLLEGE
Information
TechnologyDepartment
Laborator' %ctivit'
Create a PHP "rogram that will ma0e useof 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 controlstatements discussed in PHP
W b P i
-
8/13/2019 PHP Control Statements
35/35
Web Programming
FEU EAST ASIA COLLEGE
Information
Technology
Short .ui/ 0
1. What is $x, assuming that itsinitial value is 10 (3 pts)if($x>) {
if($x) {
if ($x "!){
$fee 2@@@;
}elseif($speed > !@){
$fee @@@;
}elseif($speed > !){
$fee D@@@;
}
echo $fee;