You are on page 1of 19

PHP Decision & Loops (Part 2 of 5) Slide 1

CHAPTER 2:
PHP Decision & Loops (2 of 5)
Topics covered:-
if, else and else if statement
switch statement
Ternary operator
do, while and for statement
break and continue statement
Nested Loops

AMIT 2043 Web Systems and Technologies


PHP Decision & Loops (Part 2 of 5) Slide 2

Decision Making
• You have learnt to make decision using if-else
statement, else if statement and switch statement
using C programming language in the course
AACS1074 and AACS1084 Programming Concept and
Design I (PCD I) and PCDII.

AMIT 2043 Web Systems and Technologies


PHP Decision & Loops (Part 2 of 5) Slide 3

Using if-else statement


<?php

/*
* Demonstrating if-else statement
*/

$basic = 5000;
$manager = true;
$allowance = 0;
if($basic > 1000 && $manager == true)
$allowance += 300;
else
$allowance += 50;

echo $basic + $allowance;


?>

Refer to code example: ifelseStatement


AMIT 2043 Web Systems and Technologies
PHP Decision & Loops (Part 2 of 5) Slide 4

Using if-else statement


<?php

/*
* Demonstrating if-else statement
*/

$basic = 5000;
$manager = true;
$allowance = 0;
if($basic > 1000)
if($manager == true)
$allowance += 300;
else
$allowance += 50;

echo $basic + $allowance;


?>

AMIT 2043 Web Systems and Technologies (2017/2018)


AACS
PHP Decision & Loops (Part 2 of 5) Slide 5

Using else if statement

$marks = 52; else if($marks >= 55)


$grade = null; $grade = 'C+';
if($marks >= 80) else if($marks >= 50)
$grade = 'A'; $grade = 'C';
else if($marks >= 75) else if($marks >= 45)
$grade = 'A-'; $grade = 'C-';
else if($marks >= 70) else if($marks >= 40)
$grade = 'B+'; $grade = 'D';
else if($marks >= 65) else
$grade = 'B'; $grade = 'F';
else if($marks >= 60)
$grade = 'B-'; echo $grade;

AMIT 2043 Web Systems and Technologies


PHP Decision & Loops (Part 2 of 5) Slide 6

Using switch statement

$marks = 52; case $marks >= 55: $grade = 'C+';


switch($marks){ break;
case $marks >= 80: $grade = 'A'; case $marks >= 50: $grade = 'C';
break; break;
case $marks >= 75: $grade = 'A-'; case $marks >= 45: $grade = 'C-';
break; break;
case $marks >= 70: $grade = 'B+'; case $marks >= 40: $grade = 'D';
break; break;
case $marks >= 65: $grade = 'B'; default: $grade = 'F';
break; }
case $marks >= 60: $grade = 'B-';
break; echo $grade;

AMIT 2043 Web Systems and Technologies


PHP Decision & Loops (Part 2 of 5) Slide 7

Ternary Operator (?)


• The symbol for the ternary operator is ?
• In C programming language, this is called conditional-if
statement.
• This ternary operator is used as an alternative to simple if-else
statement that involves two choices.
• Example:
/*
* Demonstrating the use of Ternary Operator (?)
*/
$sales = 9000;
$allowance = 0;
($sales > 10000) ? $allowance = 1000 : $allowance = 200;
echo $sales + $allowance;

AMIT 2043 Web Systems and Technologies


PHP Decision & Loops (Part 2 of 5) Slide 8

Using Decision to display greetings


$hour = date('G');
$year = date('Y');
if($hour >= 5 && $hour < 12){
echo "<h1>Good Morning</h1>";
}else if ($hour >= 12 && $hour < 18){
echo "<h1>Good Afternoon</h1>";
}else if ($hour >= 18 && $hour < 22){
echo "<h1>Good Evening</h1>";
}else{
echo "<h1>Good Night</h1>";
}

$leapYear = false;
if((($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0)){
$leapYear = true;
}
echo "<p>Did you know that $year is " . ($leapYear ? " " : " not ") . "a leap year?</p>";
?> Refer to code example: displayGreeting

AMIT 2043 Web Systems and Technologies


PHP Decision & Loops (Part 2 of 5) Slide 9

Using Decision to display greetings


• Did you noticed something wrong with the greeting
displayed?
• Why? Wrong timezone.
• Use date_default_timezone_set(“<timezone specifier>”);

• Example:
date_default_timezone_set(“Asia/Kuala_Lumpur”);

• Supply the above statement before the use of any


date/time functions.

AMIT 2043 Web Systems and Technologies


PHP Decision & Loops (Part 2 of 5) Slide 10

Date and Time Functions


Function Description
checkdate() A date is valid if:
✓ month is between 1 and 12 inclusive
✓ day is within the allowed number of days for the particular
month
✓ year is between 1 and 32767 inclusive

date_default_timezone_get() Returns the default time zone


date_default_timezone_set() Sets the default time zone
date() Formats a local time/date

getdate() Returns an array that contains date and time information for a
Unix timestamp
[seconds] - seconds ,[minutes] - minutes , [hours] - hours
[mday] - day of the month , [wday] - day of the week , [year] - year
[yday] - day of the year , [weekday] - name of the weekday ,

[month] - name of the month

localtime() Returns an array that contains the time components of a Unix


timestamp
AMIT
time()2043 Web Systems and Returns
/ mktime() Technologies
the current timestamp.
PHP Decision & Loops (Part 2 of 5) Slide 11
Date Function Formatting
Character Meaning Example
Y year as 4 digits 2011
y y year as 2 digits 11
n month as 1 or 2 digits 3
m month as 2 digits 03
F month February
M month as 3 letters Feb
j day of the month as 1 or 2 digits 5
d day of the month as 2 digits 07
l day of the week Monday
D day of the week as 3 letters Mon
g hour, 12-hour format as 1 or 2 digits 6
G hour, 24-hour format as 1 or 2 digits 18
h hour, 12-hour format as 2 digits 06
H hour, 24-hour format as 2 digits 18
i minutes
s seconds
AMIT
a
2043 Web Systems and Technologies
am or pm
PHP Decision & Loops (Part 2 of 5) Slide 12

Useful Reference Link


For a list of supported timezone
• http://www.php.net/manual/en/timezones.php

Using date() function?


For a list of supported date format
• http://www.php.net/manual/en/datetime.formats.da
te.php
For a list of supported time format
• http://www.php.net/manual/en/datetime.formats.ti
me.php

AMIT 2043 Web Systems and Technologies


PHP Decision & Loops (Part 2 of 5) Slide 13

Looping
• Allows to run the same block of code again and again until
a certain condition is met.
• The condition is an expression. If the expression evaluated
to true, the loop continues. Otherwise, the loop exit.
• Three (3) main types of loops:-
• while
• do … while
• for

• And another special loops called foreach() loop used


when working with arrays. This will be covered in (part 4
of 5).
• foreach()

AMIT 2043 Web Systems and Technologies


PHP Decision & Loops (Part 2 of 5) Slide 14

while() loop
$countDown = 10;

while($countDown >= 0){


echo "Count Down : " . $countDown . "<br>";
$countDown--;
}

echo "Welcome to the year 2014";

Refer to code example: looping

AMIT 2043 Web Systems and Technologies


PHP Decision & Loops (Part 2 of 5) Slide 15

do … while() loop

$total = 0;
$radius = 1;
$count = 1;
$area = 0;
define("PI", 3.14159);

echo "--This program is to compute total area of circles--<br>";


do{

$area = PI * pow($radius,2);
$total += $area;
echo "[$count] Calculated area of a circle with radius $radius<br>";
echo "Total area of circles : " . $total . "<br>";
$radius += 1;
$count += 1;
}while($radius <= 5); Refer to code example: looping
AMIT 2043 Web Systems and Technologies
PHP Decision & Loops (Part 2 of 5) Slide 16

for() loop

$total = 0;
$radius = 1;
$count = 1;
$area = 0;
define("PI", 3.14159);

echo "--This program is to compute total area of circles--<br>";


for($i = 0; $i <= 4; $i++){
$area = PI * pow($radius,2);
$total += $area;
echo "[$count] Calculated area of a circle with radius $radius<br>";
echo "Total area of circles : " . $total . "<br>";
$radius += 1;
$count += 1;
}
Refer to code example: looping
AMIT 2043 Web Systems and Technologies
PHP Decision & Loops (Part 2 of 5) Slide 17

Escaping for() loop using break statement

$total = 0;
$radius = 1;
$count = 1;
$area = 0;
define("PI", 3.14159);

for($i = 0; $i <= 5; $i++){


$area = PI * pow($radius,2);
$total += $area;
echo "[$count] Calculated area of a circle with radius $radius<br>";
echo "Total area of circles : " . $total . "<br>";
$radius += 1;
$count += 1;
if($radius == 3) break;
}
Refer to code example: looping
AMIT 2043 Web Systems and Technologies
PHP Decision & Loops (Part 2 of 5) Slide 18

Use of the continue statement in for() loop

$total = 0;
$radius = 1;
$area = 0;
define("PI", 3.14159);

for($radius = 1; $radius <= 6; $radius++){


if($radius == 4) continue;
$area = PI * pow($radius,2);
$total += $area;
echo "Calculated area of a circle with radius $radius<br>";
echo "Total area of circles : " . $total . "<br>";
}

Refer to code example: looping


AMIT 2043 Web Systems and Technologies
PHP Decision & Loops (Part 2 of 5) Slide 19

Use of nested loops

$asteriskArray;
for($i = 1; $i <= 6; $i++){
echo "<br>";
for($j = 1; $j <= 10; $j++){
$asteriskArray[$i][$j] = "*";
echo $asteriskArray[$i][$j];
}
}

Refer to code example: looping


AMIT 2043 Web Systems and Technologies

You might also like