Chapter 2-2 PHP Decision and Loops
Chapter 2-2 PHP Decision and Loops
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
/*
* Demonstrating if-else statement
*/
$basic = 5000;
$manager = true;
$allowance = 0;
if($basic > 1000 && $manager == true)
$allowance += 300;
else
$allowance += 50;
/*
* Demonstrating if-else statement
*/
$basic = 5000;
$manager = true;
$allowance = 0;
if($basic > 1000)
if($manager == true)
$allowance += 300;
else
$allowance += 50;
AMIT
AACS 2043 Web Systems and Technologies (2017/2018)
Slide 5
PHP Decision & Loops (Part 2 of 5)
Using else if statement
$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
• Example:
date_default_timezone_set(“Asia/Kuala_Lumpur”);
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 ,
AMIT
time()2043 Web Systems andReturns
/ mktime() Technologies
the current timestamp.
Slide 11
PHP Decision & Loops (Part 2 of 5)
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
amWeb
or pm Systems and Technologies
Slide 12
PHP Decision & Loops (Part 2 of 5)
Useful Reference Link
For a list of supported timezone
• http://www.php.net/manual/en/timezones.php
$total = 0;
$radius = 1;
$count = 1;
$area = 0;
define("PI", 3.14159);
$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
Slide 16
PHP Decision & Loops (Part 2 of 5)
for() loop
$total = 0;
$radius = 1;
$count = 1;
$area = 0;
define("PI", 3.14159);
$total = 0;
$radius = 1;
$count = 1;
$area = 0;
define("PI", 3.14159);
$total = 0;
$radius = 1;
$area = 0;
define("PI", 3.14159);
$asteriskArray;
for($i = 1; $i <= 6; $i++){
echo "<br>";
for($j = 1; $j <= 10; $j++){
$asteriskArray[$i][$j] = "*";
echo $asteriskArray[$i][$j];
}
}