PEMROGRAMAN WEB - PHP
Conditional & Looping
Topik Bahasan
• If ..... else Statement
• Switch Satement
• For Loop Statement
• While Loop Statement
• For Loop Statement
Conditional Statements
In PHP we have the following conditional statements:
• if statement - use this statement to execute some code only if
a specified condition is true
• if...else statement - use this statement to execute some code
if a condition is true and another code if the condition is false
• if...elseif....else statement - use this statement to select one
of several blocks of code to be executed
• switch statement - use this statement to select one of many
blocks of code to be executed
• The if Statement
• Use the if statement to execute some code only if a specified condition is
true.
• Syntax:
if (condition) code to be executed if condition is true;
• will output "Have a nice weekend!" if the current day is Friday
• Use break to prevent the code from running into the next case
automatically. The default statement is used if no match is
found.
PHP Switch Statement
• Conditional statements are used to perform different actions
based on different conditions.
• Use the switch statement to select one of many blocks of code
to be executed.
6
PHP Loop Types
• Loops in PHP are used to execute the same block of code a
specified number of times.
• PHP supports following four loop types:
• for - loops through a block of code a specified number of times.
• while - loops through a block of code if and as long as a specified
condition is true.
• do...while - loops through a block of code once, and then repeats
the
loop as long as a special condition is true.
• foreach - loops through a block of code for each element in an
array.
The for loop statement
• The for statement is used when you know how many times you
want to execute a statement or a block of statements.
• Syntax
• Example
• Result
The while loop statement
• The while statement will execute a block of code if and as long as a
test expression is true.
• Syntax
• Example
• Result
The do...while loop statement
• The do...while statement will execute a block of code at least once -
it then will repeat the loop as long as a condition is true.
• Syntax
• Example
• Result
The foreach loop statement
• The foreach statement is used to loop through arrays.
• For each pass the value of the current array element is assigned to
$value and the array pointer is moved by one and in the next pass
next element will be processed.
• Syntax
• Example
• Result
PHP
Arrays
• An array is a special variable, which can store multiple values
in one single variable.
• There are three kind of arrays:
• Numeric array - An array with a numeric index
• Associative array - An array where each ID key is associated with
a value
• Multidimensional array - An array containing one or more arrays
Numeric Arrays
• A numeric array stores each array element with a numeric
index.
• There are two methods to create a numeric array.
• the index are automatically assigned (the index starts at
0).
$cars=array("Saab","Volvo","BMW","Toyota");
• assign the index manually.
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
• Example: access the variable values by referring to the array
name and index.
• Output:
Saab and Volvo are Swedish
cars.
Associative Arrays
• An associative array, each ID key is associated with a value.
• Example 1: an array to assign ages to the different persons.
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
• Example 2: shows a different way of creating the array.
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
• The ID keys can be used in a script.
• Output: Peter is 32 years old.
Multidimensional Arrays
• In a multidimensional array, each element in the main array
can also be an array. And each element in the sub-array can be
an array, and so on.
• Create a multidimensional array, with automatically assigned
ID keys.
• The array above is similar to
• Example: Lets try displaying a single value from the array above.
• Output: Is Megan a part of the Griffin family?
Latihan Soal
Tampilkan nama & nrp mahasiswa ke layar, dengan kondisi seperti berikut:
1. IPK mahasiswa > 3.00 dan < 3.25
2. Mahasiswa berambut hitam dan lurus
3. Mahasiswa memiliki berat badan 50 kg dan tinggi badan 160 cm
4. Mahasiswa tidak sedang menjalani cuti
• NRP 1 – 5: if-else
• NRP 6-10: switch-case
• NRP 11-15: do-while
• NRP 16-20: for-loop
• NRP 21-30:while
Abi
<?php
$mhs = array(
array(’nama’=>”Labba”,’nrp’=>61,’ipk’=>3.15,’rambut’=>array(”hitam”,”lurus”),’bb’=>50,’tb’=>160,’st
atus’=>”aktif”),
array(’nama’=>”Awwabi”,’nrp’=>62,’ipk’=>3.35,’rambut’=>array(”coklat”,”berombak”),’bb’=>70,’tb’=>17
0,’status’=>”cuti”),
array(’nama’=>”Abi”,’nrp’=>63,’ipk’=>3.5,’rambut’=>array(”merah”,”lurus”),’bb’=>90,’tb’=>180,’statu
s’=>”aktif”),
);
for($i=0;$i<$mhs.lenght();$i++) if($mhs[$i]
[’ipk’]>3 && $mhs[$i][’ipk’]<3.25)
if($mhs[$i][’rambut’][0]==”hitam” &&
$mhs[$i][’rambut’][1]==”lurus”)
if($mhs[$i][’bb’]==50 &&
$mhs[$i][’tb’]==160)
if($mhs[$i][’status’]!
=”cuti”)
echo ”nama : ”.
$mhs[$i]
[’nama’].”
<br/>nrp :
”.$mhs[$i][’nrp’].”<hr/>”;
?>
RANGGA
<?php
$mhs = array(
array(’n
ama’=>”L
abba”,’n
rp’=>61,
’ipk’=>3
.15,’ram
but’=>ar
ray(”hit
am”,”lur
us”),’bb
’=>50,’t
b’=>160,
’st
atus’=>”aktif”)
;
$i = 0;
do{
if($mhs[$i][’ipk’]>3 && $mhs[$i][’ipk’]<3.25){ if($mhs[$i][’rambut’]
[0]==”hitam” && $mhs[$i][’rambut’][1]==”lurus”){
?> if($mhs[$i][’bb’]==50 && $mhs[$i][’tb’]==160){
if(!$mhs[$i][“isCuti”]){
echo $mhs[$i][“nrp”].” “.$mhs[$i]