Decisions, Loops, and Arrays
Achmad Arwan, S.Kom
IF 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 Statements
• 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;• <html>
<body><?php$d=date("D");if ($d=="Fri") echo "Have a nice weekend!";?></body></html>
The if...else Statement
• Use the if....else statement to execute some code if a condition is true and another code if a condition is false.
• if (condition) code to be executed if condition is true;else code to be executed if condition is false; <html><body><?php$d=date("D");if ($d=="Fri") echo "Have a nice weekend!";else echo "Have a nice day!";?></body></html>
The if...else Statement
• If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:
• <html><body>
<?php$d=date("D");if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; }?>
</body></html>
The if...elseif....else Statement
• Use the if....elseif...else statement to select one of several blocks of code to be executed.
• if (condition) code to be executed if condition is true;elseif (condition) code to be executed if condition is true;else code to be executed if condition is false;
<html><body><?php$d=date("D");if ($d=="Fri") echo "Have a nice weekend!";elseif ($d=="Sun") echo "Have a nice Sunday!";else echo "Have a nice day!";?></body></html>
The PHP Switch Statement
• Use the switch statement to select one of many blocks of code to be executed.
• switch (n){case label1: code to be executed if n=label1; break;case label2: code to be executed if n=label2; break;default: code to be executed if n is different from both label1 and label2;}
<html><body><?php$x=1;switch ($x){case 1: echo "Number 1"; break;case 2: echo "Number 2"; break;case 3: echo "Number 3"; break;default: echo "No number between 1 and 3";}?></body></html>
PHP Loops
• In PHP, we have the following looping statements:• while - loops through a block of code while a specified
condition is true• do...while - loops through a block of code once, and
then repeats the loop as long as a 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
The while Loop
• The while loop executes a block of code while a condition is true.Syntax
• while (condition) { code to be executed; }
<html><body>
<?php$i=1;while($i<=5) { echo "The number is " . $i . "<br />"; $i++; }?>
</body></html>
The do...while Statement
• The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.Syntax
• do { code to be executed; }while (condition);
<html><body>
<?php$i=1;do { $i++; echo "The number is " . $i . "<br />"; }while ($i<=5);?>
</body></html>
The for Loop
• The for loop is used when you know in advance how many times the script should run.Syntax
• for (init; condition; increment) { code to be executed; }
• Parameters:• init: Mostly used to set a counter (but can be any code to
be executed once at the beginning of the loop)• condition: Evaluated for each loop iteration. If it evaluates
to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
• increment: Mostly used to increment a counter (but can be any code to be executed at the end of the iteration)
<html><body>
<?phpfor ($i=1; $i<=5; $i++) { echo "The number is " . $i . "<br />"; }?>
</body></html>
The foreach Loop
• The foreach loop is used to loop through arrays.Syntax
• foreach ($array as $value) { code to be executed; }
• For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.
<html><body>
<?php$x=array("one","two","three");foreach ($x as $value) { echo $value . "<br />"; }?>
</body></html>
Arrays
• An array is a set of variables that all have the same name but a different index.
• Each member of the array is called an element.
• Each element's name must be followed by its index in square brackets:
• $nama[1]=“Adi”;• $nama[2]=“Dina”;
Initialization of Arrays
• $author[0]="William Shakespeare"; • $author = array ("William Shakespeare",
"Franz Kafka"); • $author = array (1=>"William Shakespeare",
"Franz Kafka");• $author = array (“wl”=>"William
Shakespeare", “Fk”=>"Franz Kafka");
Iterating through an Array
• for ($counter=1; $counter<51; $counter++) { echo"<BR>$author[$counter]"; }
• Or• $counter = 1; • while ($counter < 51) { • echo"<br> $author[$counter]"; • $counter = $counter + 1; • }
current() and key() Functions
• PHP uses a pointer to keep track of which element it's at when it moves through an array.
• The pointer indicates the element that is currently being used by the script.
• You can use the current() function to view the value of that element.
• you can use the key() function to find out its index value. (Key is another name for index.)
current() and key() sample
• $director[4]="Orson Welles"; $director[1]="Carol Reed"; $director[93]="Fritz Lang"; $director[24]="Jacques Tourneur";$current_index_value = key($director); echo ($current_index_value);$current_contents = current($director); echo ($current_contents);
next() and prev() Functions
• These functions enable you to navigate through arrays by moving the pointer to the next or previous element in the array.
• next() move to next array• previous() move to previous array
next() and prev() Functions
$director[4]="Orson Welles"; $director[1]="Carol Reed"; $director[93]="Fritz Lang"; $director[24]="Jacques Tourneur"; $director[]="Alfred Hitchcock";next($director); $current_index_value = key($director);echo ($current_index_value);
prev($director); $current_index_value = key($director); echo ($current_index_value);
list() and each() Functions
• use the list() and each() functions to return only the elements in the array that contain data.
• Syntaxwhile (list($element_index_value, $element_contents) = each($director)){ echo "<br>$element_index_value - $element_contents"; }
list() and each() Functions sample
• $director[4]="Orson Welles"; $director[1]="Carol Reed"; $director[93]="Fritz Lang"; $director[24]="Jacques Tourneur";
• while (list($MickeyMouse, $DonaldDuck) = each ($Director)){ echo "<BR>$MickeyMouse - $DonaldDuck"; }
sort() function
• sort() is the most basic of the sorting functions. It takes the contents of the array and sorts them into alphabetical order.
• The function requires only an array name to sort the array:
• sort(ArrayName)
sort() sample
<?php$director = array ("Orson Welles","Carol Reed", "Fritz Lang",
"Jacques Tourneur");echo “Before sort”;while (list($IndexValue, $DirectorName) = each ($Director))
{ echo "<BR>$IndexValue - $DirectorName"; }sort($director);echo “Before sort”;while (list($IndexValue, $DirectorName) = each ($Director))
{ echo "<BR>$IndexValue - $DirectorName"; }?>
Multidimentional array
Title Price Number
rose 1.25 15
daisy 0.75 25
orchid 1.15 7
<?php $shop = array( array( Title => "rose", Price => 1.25, Number => 15 ), array( Title => "daisy", Price => 0.75, Number => 25, ), array( Title => "orchid", Price => 1.15, Number => 7 ) );?>
Multidimentional Arrays
<?php echo "<h1>Manual access to each element</h1>";echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />";echo $shop[1][0]." costs ".$shop[1][1]." and you get ".$shop[1][2]."<br />";echo $shop[2][0]." costs ".$shop[2][1]." and you get ".$shop[2][2]."<br />";echo "<h1>Using loops to display array elements</h1>";echo "<ol>";for ($row = 0; $row < 3; $row++){ echo "<li><b>The row number $row</b>"; echo "<ul>"; for ($col = 0; $col < 3; $col++) { echo "<li>".$shop[$row][$col]."</li>"; } echo "</ul>"; echo "</li>";}echo "</ol>";?>
<?phpecho “Thanks”;?>