Title: PHP Control Structures Part 2
1PHP Control Structures Part 2
2Conditional Complexity
- You can imagine that conditionals can get quite
complex very quickly. - Imagine a scenario where you need to perform a
specific action based upon the contents of a
menu with 12 .
3A Complex if-elsif-else Conditional
// Get the horoscope sign. if (month 1) //
January if (day 'Caprico1rn' else sign 'Aquarius'
elseif (month 2) // February if (day
'Pisces' elseif (month 3) if
(day sign 'Aries' elseif (month 4)
if (day sign 'Taurus' elseif (month 5)
if (day sign 'Gemini' elseif (month 6)
if (day sign 'Cancer' elseif (month 7)
if (day sign 'Leo' elseif (month 8)
if (day sign 'Virgo' elseif (month 9)
if (day sign 'Libra' elseif (month 10)
if (day sign 'Scorpio' elseif (month
11) if (day else sign 'Sagittarius' elseif
(month 12) if (day 'Sagittarius' else sign
'Capricorn'
4Switch conditional
- The switch() conditional can make code cleaner
and easier to read.
switch (name) case "afonzarelli" role
'admin' break case "rmouth" role
'flunky' break case "pwebber" role
'flunky' break default role ''
echo 'Name undefined.' break
if (name) 'afonzarelli') role
'admin' elseif (name 'rmouth') role
'flunky' elseif(name 'pwebber') role
'flunky' else role '' echo 'Name
undefined.'
These two constructs do exactly the same thing.
5Switch conditional
- switch() uses the value of z as its condition,
first checking if z "value1", and if so,
executing statement(s) and then breaking out of
the switch() conditional.
switch (z) case "value1" statement1(s)
break
case "value2" statements(s) break
default statement(s) break
6Looping Control Structures
- Loops provide a mechanism for executing a section
of code repeatedly. - PHP supports three kinds of loops for, while,
and foreach.
7for Loop
- for loops perform specific statements for a
determined number of iterations. - for (initial expression, condition closing
expression) - statement(s)
-
- Example
- for (v 1 v
- print "v"
8while Loop
- while loops executes the loop until the condition
is false. - Used frequently for retrieving values from a
database or file. - i 1
- while (i
- echo i / printed value is
- i before the increment
- (post-increment) /
9do-while Loop
- Guarantees that the statements are executed at
least once - z1
- do
- print "z"
- z
- while (z 0)
Executes this statement, even though z1
10foreach Loop
- Used extensively to grab and manipulate values
from arrays. - There are two syntaxes the second is a minor but
useful extension of the first . - foreach (array_expression as value)
statementforeach (array_expression as key
value) statement - The first form loops over the array given by
array_expression. On each loop, the value of the
current element is assigned to value and the
internal array pointer is advanced by one - The second form does the same thing, except that
the current element's key will be assigned to the
variable key on each loop.
11Break, Die, Exit, Continue
- break exits the current structure, be it a
switch, an if-else conditional, or a loop. - continue terminates the current iteration of the
loop. Any remaining statements within the loop
aren't executed, and the loop's condition is
checked again to see if the loop should be
entered.
12Exit and Die
- exit terminates the execution of the script. All
code after exit or die remains unexecuted
(including HTML). die is an alias for exit - filename '/path/to/data-file.txt'
- file fopen(filename, 'r') or exit("color'red'unable to open file
(filename)")
13Example of Continue
- for (counter 0 counter
-
- test counter
- print "counter counter
" - if( test 6 )
-
- echo "skipped...
" - continue / Jumps to top w/o executing
the - rest of the statements in
this - conditional /
-
- / This line will only print if
- test does not contain 6 /
- print "test test
" -
- print "DONE!"