Control Structures
Programming PHP - Chapter 2. Language Basics (pages 46-54)
Overview
- Much like C/Perl
- Some interesting things we can do when mixed with HTML
Selection Structures
PHP has 3 selection structures:
- If-Then-Else
- Ternary Operator
- Switch
If-Then-Else
Much like C/Perl. Else-If can be written as either else if or elseif. Curly braces optional (but encouraged).
- $age0 = 15;
- $age1 = 19;
- $age2 = 21;
- <html>
- <head>
- <title>If-Then-Else</title>
- </head>
- <body>
- <h3>Age $age0 </h3>
- if($age0 < 18) {
- print "<p>Sorry, nothing for you - go play with your toys</p>";
- }
- <h3>Age $age1 </h3>
- // HTML generation inline
- if($age1 < 18) {
- print "<p>Sorry, nothing for you - go play with your toys</p>";
- } elseif ($age1 < 21) {
- print "<p>Smoke</p>";
- } else {
- print "<p>Smoke and Drink</p>";
- }
- <h3>Age $age2 </h3>
- if($age2 < 18) {
- <p>Sorry, nothing for you - go play with your toys</p>
- } elseif ($age2 < 21) {
- <p>Smoke</p>
- } else {
- <p>Smoke and Drink</p>
- }
- </body>
- </html>
Ternary Operator
Just like other languages:
- $s1 = "A";
- $s2 = "B";
- $s3 = "C";
- $s4 = "D";
- $s5 = "F";
- <html>
- <head>
- <title>Ternary Operator</title>
- </head>
- <body>
- <p>s1's grade is (($s1=='A')||($s1=='F'))?"an":"a" $s1 </p>
- <p>s2's grade is (($s2=='A')||($s2=='F'))?"an":"a" $s2 </p>
- <p>s3's grade is (($s3=='A')||($s3=='F'))?"an":"a" $s3 </p>
- <p>s4's grade is (($s4=='A')||($s4=='F'))?"an":"a" $s4 </p>
- <p>s5's grade is (($s5=='A')||($s5=='F'))?"an":"a" $s5 </p>
- </body>
- </html>
Switch
Can work on various types (String, ints, etc...). Can use a variable as a case label.
- <html>
- <head>
- <title>Switch</title>
- </head>
- <body>
- $drink = 21;
- $vote = 18;
- // variable to switch on
- $var = 'Rasmus';
- switch ($var) {
- case 'foo':
- print "how boring";
- break;
- case $vote:
- print "go vote";
- break;
- case $drink:
- Go drink
- break;
- case "Rasmus":
- print "Hello, Rasmus";
- break;
- default:
- print "Say what?";
- }
- </body>
- </html>
Repetition Structures
PHP has 4 repetition structures:
- While
- Do-While
- For
- Foreach
While Loop
PHP's while loop follows normal conventions:
- <html>
- <head>
- <title>While Loop</title>
- </head>
- <body>
- $i = 10;
- while($i > 0) {
- echo "<p>", $i, "</p>";
- $i--;
- }
- $i = 5;
- while ($i > 0) {
- $i--;
- <p>Blah</p>
- }
- </body>
- </html>
Do While Loop
Do while is the same as you would expect - don't forget the semicolon.
- <html>
- <head>
- <title>Do While Loop</title>
- </head>
- <body>
- $i = 10;
- do {
- echo "<p>", $i, "</p>";
- $i--;
- } while ($i > 0);
- $i = 5;
- do {
- $i--;
- <p>Blah</p>
- } while ($i > 0);
- </body>
- </html>
For Loop
Normal initialize, condition, update format:
- <html>
- <head>
- <title>For Loop</title>
- </head>
- <body>
- for($i = 10; $i > 0; $i--) {
- echo "<p>", $i, "</p>";
- }
- for($i = 10; $i > 0; $i--) {
- <p>Blah</p>
- }
- </body>
- </html>
Foreach Loop
PHP has a foreach loop like the other scripting languages we have already looked discussed. Syntax is a bit different - uses a
foreach($array as $element) type of notation:
- $flintstones = array("Fred", "Barney", "Wilma", "Betty");
- $jetsons = array("George", "Elroy", "Jane", "Judy");
- <html>
- <head>
- <title>Foreach Loop</title>
- </head>
- <body>
- foreach($flintstones as $person) {
- echo "<p>", $person, "</p>";
- }
- foreach($jetsons as $person) {
- <p>$person </p>
- }
- </body>
- </html>







