WEB DESIGN 2 USING PHP
EXPRESSIONS AND
CONTROL FLOW IN PHP
EXPRESSIONS
• Expressions evaluate to a value. The value
can be a string, number, boolean, etc...
• Expressions often use operations and
function calls, and there is an order of
evaluation when there is more than one
operator in an expression
• Expressions can also produce objects like
arrays
High
Low
OPERATORS OF NOTE
• Increment / Decrement ( ++ -- )
• String concatenation ( . )
• Equality ( == != )
• Identity ( === !== )
• Ternary ( ? : )
• Side-effect Assignment ( += -= .= etc.)
INCREMENT / DECREMENT
• These operators allow you to both retrieve
and increment / decrement a variable
• They are generally avoided in civilized
code.
$x = 12;
$y = 15 + $x++; x is 13 and y is
echo "x is $x and y is 27
$y \n";
INCREMENT / DECREMENT
• These operators allow you to both retrieve
and increment / decrement a variable
• They are generally avoided in civilized
code.
$x = 12;
$y = 15 + $x; x is 13 and y is
$x = $x + 1; 27
echo "x is $x and y is
$y \n";
STRING CONCATENATION
• PHP uses the period character for
concatenation because the plus character
would instructor PHP to to the best it could
do to add the two things together,
converting if necessary.
$a = 'Hello ' . Hello
'World!'; World!
echo $a . "\n";
LOGICAL EXPRESSION
<?php
echo "a: [" . (20 > 9) . The output from
"]<br />"; this code is as
echo "b: [" . (5 == 6) . follows:
"]<br />"; a: [1]
b: []
echo "c: [" . (1 == 0) . c: []
"]<br />"; d: [1]
echo "d: [" . (1 == 1) .
"]<br />";
?>
EQUALITY VERSUS IDENTITY
• The equality operator (==) in PHP is far
more agressive than in most other
languages when it comes to data
conversion during expression evaluation.
if ( 123 == "123" ) print ("Equality 1\n");
if ( 123 == "100"+23 ) print ("Equality 2\
n");
if ( FALSE == "0" ) print ("Equality 3\n");
if ( (5 < 6) == "2"-"1" ) print ("Equality
4\n");
if ( (5 < 6) === TRUE ) print ("Equality 5\
n");
TERNARY
• The ternary operator comes , It allows
conditional expressions. It is like a one-
line if-then-else . Like all "contraction"
syntaxes, we use it carefully.
$www = 123;
$msg = $www > 100 ? "Large" : "Small"
;
echo "First: $msg \n";
$msg = ( $www % 2 == 0 ) ? "Even" :
"Odd";
echo "Second: $msg \n"; First: Large
$msg = ( $www % 2 ) ? "Odd" : "Even";
Second: Odd
echo "Third: $msg \n"; Third: Odd
SIDE-EFFECT ASSIGNMENT
• These are pure contractions. Civilized
programmers use them sparingly.
echo “<br/>";
$out = "Hello";
$out = $out . " ";
$out .= "World!";
$out .= “<br/>"; Hello
echo $out;
$count = 0;
World!
$count += 1; Count: 1
echo "Count: $count\n";
CONTROL STRUCTURES
We use condition to perform different actions
for different decisions.
In PHP we have the following conditional
statements:
if statement - executes some code only if a
specified condition is true
if...else statement - executes some code if a
condition is true and another code if the
condition is false
if...elseif....else statement - selects one of
several blocks of code to be executed
switch statement - selects one of many
blocks of code to be executed
TWO-WAY
USING X=4
ELSE :
n ye
o s
$x = 4; x>2
if ($x > 2) {
print "Bigger
print 'Smaller' print 'Bigger'
<br/>";
} else {
print "Smaller
<br/>";
}
print "All done print 'All
<br/”; > Done'
MULTI-
WAY
ye
$x = 7;
s
x<2 print 'Small'
if ( $x < 2 ) {
print n
"Small<br/>"; o
} elseif ( $x < 10 ) ye
{ s
x<10 print 'Medium'
print "Medium\n";
n
} else {
o
print "LARGE\n";
}
print 'LARGE'
print "All Done”
done\n";
print 'All
Done'
CURLY BRACES ARE NOT
REQUIRED
if ($page == "Home") echo "You selected
Home";
elseif ($page == "About") echo "You selected
About";
elseif ($page == "News") echo "You selected
News";
elseif ($page == "Login") echo "You selected
Login";
elseif ($page == "Links") echo "You selected
Links";
if ($page == "Home") { echo "You selected
Home"; }
elseif ($page == "About") { echo "You selected
About"; }
elseif ($page == "News") { echo "You selected
News"; }
elseif ($page == "Login") { echo "You selected
Login"; }
elseif ($page == "Links") { echo "You selected
Links"; }
SWITCH SYNTAX
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all
labels;
}
switch ($page)
{
case "Home":
echo "You selected Home";
break;
case "About":
echo "You selected About";
break;
case "News": echo "You selected
News";
break;
case "Login": echo "You selected
Login";
break;
case "Links": echo "You selected
Links";
break; I am not a big fan of
} switch
LOOPING STRUCTURES
In PHP, we have the following looping
statements:
while - loops through a block of code as
long as the specified condition is true
do...while - loops through a block of
code once, and then repeats the loop as
long as the specified condition is true
for - loops through a block of code a
specified number of times
foreach - loops through a block of code
for each element in an array
$fuel = 10;
while ($fuel > 1) {
print "Vroom vroom\
n";
}
A while loop is a
"zero-trip" loop with
the test at the top. $fuel = 10;
before the first while ($fuel > 1) {
iteration starts. We print "Vroom vroom\
hand construct the n";
iteration variable to $fuel = $fuel -1;
implement a counted }
loop.
$count = 1;
do {
echo "$count times 5 is " . $count * 5;
echo "\n";
} while (++$count <= 5);
1 times 5 is 5
A do-while loop is a 2 times 5 is
"one-trip" loop with 10
the test at the 3 times 5 is
15
bottom after the first
4 times 5 is
iteration completes.
20
5 times 5 is
25
for($count=1; $count<=6; $count++ ) {
echo "$count times 6 is " . $count * 6;
echo "\n"; 1 times 6 is 6
} 2 times 6 is
12
3 times 6 is
A for loop is the 18
simplest way to 4 times 6 is
construct a counted 24
loop. 5 times 6 is
30
6 times 6 is
Loop runs while TRUE
(top-test)
Run after each
Before loop
iteration.
starts
for($count=1; $count<=6; $count++ ) {
echo "$count times 6 is " . $count * 6;
echo "\n"; 1 times 6 is 6
} 2 times 6 is
12
3 times 6 is
18
A for loop is the 4 times 6 is
simplest way to 24
construct a counted 5 times 6 is
loop. 30
6 times 6 is
LOOPING THROUGH AN ARRAY
<?php
$stuff = array("name" => "Chuck",
"course" =>
"SI664");
foreach($stuff as $k => $v ) {
echo "Key=",$k," Val=",$v,"\
n";
}
?>
Key=name Val=Chuck
Key=course
Val=SI664
LOOPING THROUGH AN ARRAY
<?php
$stuff =
array("Chuck","SI664");
foreach($stuff as $k => $v ) {
echo "Key=",$k," Val=",$v,"\
n";
}
?>
Key=0 Val=Chuck
Key=1 Val=SI664
COUNTED LOOP THROUGH AN
ARRAY
<?php
$stuff = array("Chuck","SI664");
for($i=0; $i < count($stuff); $i++)
{
echo "I=",$i," Val=",
$stuff[$i],"\n";
}
?>
I=0 Val=Chuck
I=1 Val=SI664
LOOP CONTROLS
• PHP has two control structures that work
within a loop
• break - exit the loop immediately
• continue - finish the current iteration and
jump to the next iteration, starting at the
top of the loop
BREAKING OUT OF A LOOP
• The break statement ends the current loop and
jumps to the statement immediately following
the loop
• It is like a loop test that can happen anywhere in
the body of the loop
for($count=1; $count<=600; $count++ ) {
Count:
if ( $count == 5 ) break;
1
echo "Count: $count\n";
Count:
}
2
echo "Done\n";
Count:
3
Count:
FINISHING AN ITERATION
WITH CONTINUE
• The continue statement ends the current
iteration and jumps to the top of the loop and
starts the next iteration
Count:
for($count=1; $count<=10; $count++ ) { 1
if ( ($count % 2) == 0 ) continue; Count:
echo "Count: $count\n"; 3
} Count:
echo "Done\n"; 5
Count:
7
Count:
CONVERSION /
CASTING
• As PHP evaluates expressions, at times values
in the expression need to be converted from
one type to another as the computations are
done.
• PHP does aggressive implicit type conversion
(casting)
• You can also make type conversion (casting)
explicit with casting operators.
In PHP, division
CASTING forces operands to
be floating point.
PHP converts
$a = 56; $b = 12; expression values
$c = $a / $b; silently and
echo "C: $c\n"; agressively.
$d = "100" + 36.25 + TRUE;
echo "D: ". $d . "\n";
echo "D2: ". (string) $d . "\
n";
$e = (int) 9.9 - 1; C: 4.66666666667
echo "E: $e\n"; D: 137.25
$f = "sam" + 25; D2: 137.25
echo "F: $f\n"; E: 8
$g = "sam" . 25; F: 25
echo "G: $g\n"; G: sam25
EXPLICIT CASTING
SUMMARY
• Expressions
• Operators
• Conditional Structures
• Looping Structures
• Type Conversion and Casting