You are on page 1of 35

Web Engineering

Lecture 20
Control Structures & Loops
(PHP - III)

1
Logical Expression: If Statement
• Format:
if (expression)
statement;

• Example:
if ($a > $b)
echo “a is larger than b”;
• or (if you have more than one line of code)

if ($a > $b){


echo “a is larger than b”;
}
EXAMPLE
• <?php
$a = 4;
$b = 3;
if ($a > $b){
echo “a is larger than b”;
}
?>

• Boolean Operators: >, >=, <, <=, ==, !=


EXAMPLE
• <?php
$a = 3;
$b = 4;
if ($a > $b) {
echo “a is larger than b”;
}
if ($a < $b){
echo “a is not larger than b”;
}
?>
• <?php
$a = 3;
$b = 4;
if ($a > $b) {
echo “a is larger than b”;
}
else {
echo “a is not larger than b”;
}
?>
EXAMPLE
• How to deal with different cases:
• <?php
$a = 4;
$b = 4;
if ($a > $b) {
echo “a is larger than b”;
} elseif ($a == $b) {
echo “a equals to b”;
} else {
echo “a is smaller than b”;
}
?>
LOGICAL OPERATORS (AND)
• <?php
$a = 5;
$b = 4;
?>
<?php
$c = 20;
$d = 1;
if (($a > $b) && ($c > $d)){
echo “a is larger than b AND ”;
echo “c is larger than d ”;
}
?>
LOGICAL OPERATORS (OR)
• <?php
$a = 5;
$b = 4;
?>
<?php
$c = 1;
$d = 20;
if (($a > $b) || ($c > $d)){
echo “a is larger than b OR ”;
echo “c is larger than d ”;
}
?>
EXAMPLE
• If variable a is not set, then set equals to 100.
• <?php
$a = 5;
$b = 4;
?>
<?php
if (!isset($a)){
$a = 100;
}
echo $a;
?>
EXAMPLE
• If variable a is not set, then set equals to 100.
• <?php
$a = 5;
$b = 4;
?>
<?php
unset($a);
if (!isset($a)){
$a = 100;
}
echo $a;
?>
• If a is integer, then set its type equals to string.
• <?php
$a = 5;
$b = 4;
?>
<?php
if (is_int($a)) {
settype($a, “string”);
}
echo gettype($a);
?>
Logical Expressions: Switch
• <?php $a = 3; ?>
• <?php
switch ($a){
case 0:
echo “a equals 0”;
break;
case 1:
echo “a equals 1”;
break;
case 2:
echo “a equals 2”;
break;
default:
echo “a is not 0, 1 or 2”;
break;

}
?>
LOOPS
• Loops allow us to execute the code until
the condition is satisfied. There are three
main kinds of loops:

• while Loops
• for Loops
• foreach Loops
WHILE LOOPS
• The format of while loops is as under:

while (expression)
statement;

• The expression would be Boolean statement,


EXAMPLE
• <?php
$count = 0;
while ($count <= 10) {
echo $count . “, ”;
$count++;
}
echo “<br /> Count: {$count}”;
?>
EXAMPLE
• <?php
$count = 0;
while ($count <= 10) {
if ($count == 5) {
echo “FIVE”;
} else {
echo $count . “, ”;
}
$count++;
}
echo “<br /> Count: {$count}”;
?>
FOR LOOP
• The format of for loops is as under:

for (expr1, expr2, expr3)


statement;

• The expression would be boolean statement,


• Expression1 could be initializing statement.
• Expression2 could be condition/test.
• Expression3 could be increment/decrement.
EXAMPLE
• <?php
for ($count = 0; $count <= 10; $count++ )
{
echo $count . “, ”;
}
?>
FOREACH LOOP
• The format of foreach loops is as under:

foreach ($array as $value)


statement;

• The boolean test foreach loop is, do we have


items still left in array. Foreach loop will go
through every single element of an array.
• Foreach loop only works with array.
• Foreach loop works, it stores elements in array
in a temporary variable
EXAMPLE
• <?php
$ages = array(4, 8, 15, 17, 23, 42);
?>
<?php
// using each value
foreach ($ages as $age) {
echo $age . “, ”;
}
?>
• Syntax for associative array
• Foreach loop can take each array as a key
value pair.

foreach ($array as $key => $value)


statement;
EXAMPLE
• <?php
$ages = array(4, 8, 15, 16, 23, 42);
?>
• <?php
// using each key => value pair
// key for 4 is 0, key for 8 is 1 and so on…
foreach ($ages as $position => $age) {
echo $position . “: ” . $age . “<br/>”;
}
?>
• <?php
$prices = array(“Brand new computer”=>2000, “1
month in lynda.com training center”=>25, “Learning
PHP”=> “priceless”);
foreach ($prices as $key => $value) {
if (is_int($value)) {
echo $key . “: $” . $value . “<br/>”;
} else {
echo $key . “: ” . $value . “<br/>”;
}
}
?>
LOOPS: Continue
• If we didn’t want to print let say 5
• <?php
for ($count=0; $count <=10; $count++) {
if ($count == 5) {
continue;
}
echo $count . “, ”;
}
?>
• Output: 1, 2, 3, 4, 6, 7, 8, 9, 10,
• Lets say we are looking through, a list of students
in a database. We can perform lots of complex
action on it.

• But the very first thing we check, whether or not


the student is fresh man, s/w engineer jr. or
senior.

• If we turns out, the student is fresh man, we don’t


want anything else.

• Then we can have this check, are they fresh man,


continue
LOOPS: Break
• If we didn’t want to print before 5
• <?php
for ($count=0; $count <=10; $count++) {
if ($count == 5) {
break;
}
echo $count . “, ”;
}
?>
• Output: 1, 2, 3, 4,
• If we don’t want comma (,) at the end of last
number.
• <?php
for ($count = 0; $count <= 10; $count++) {
echo $count;
if ($count == 10) {
break;
}
echo “, ”;
}
?>
• Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
POINTERS
• Computer maintains the pointer, that’s
points to the current value of an array. By
default its always the first value.

• When we looping through array, computer


moves that pointer along the array to get
each value.
EXAMPLE
• <?php
$ages = array(4, 8, 15, 16, 23, 42);
?>
<?php
echo “1: ” . current($ages) . “<br />”;
// that will give us, the current place, the
pointer is pointing.
?>
• Output: 1: 4
• <?php
$ages = array(4, 8, 15, 16, 23, 42);
?>
<?php
echo “1: ” . current($ages) . “<br />”;
next($ages); // that will advance the pointer.
echo “2: ” . current($ages) . “<br />”;
?>
• Output: 1: 4
2: 8
• <?php
$ages = array(4, 8, 15, 16, 23, 42);
?>
<?php
echo “1: ” . current($ages) . “<br />”;
next($ages);
echo “2: ” . current($ages) . “<br />”;
reset($ages);
echo “3: ” . current($ages) . “<br />”;
?>
• Output: 1: 4
2: 8
3: 4
EXAMPLE
• <?php
$ages = array(4, 8, 15, 16, 23, 42);
?>
<?php
// while loop that moves the array pointer
while ($age = current($ages)) {
echo $age . “, ”;
next($ages);
}
?>
• Output: 4, 8, 15, 16, 23, 42,
NOTE
• When we start working with databases,
this is the format we going to take. We
grasp bunch of data out of our database.
That’s going to be an array. We can than
move through by assigning a value to it.

• Moving through each time, letting the


pointer update its own. So its keep moving
on the database. Keep working on each
successive row it returns.
• Let say we have a database of products.
We can have product1 output. And then
advance to next product in the database.
The next item in the array and output that.
It makes lot more sense when we are
working with database.

You might also like