Arab Open University Faculty of computer Studies M275 - Web Development using PHP and MySQL Chapter 6 Flow Control Functions in PHP Summary This is a supporting material to chapter 6. This summary will never substitute original text book. Haifaa Elayyan 2/8/2013
I. Switching Flow 1) The if Statement Why it being used : Flow Control Functions in PHP. The if statement is a way of controlling the execution of a statement that follows it (that is, a single statement or a block of code inside braces). How it works : The if statement evaluates an expression found between parentheses. If this expression results in a true value, the statement is executed. Otherwise, the statement is skipped entirely. if (expression) { // code to execute if the expression evaluates to true 2) Using the else Clause with the if Statement When working with an if statement, you might want to define an alternative block of code that should be executed if the expression you are testing evaluates to false. You can do this by adding else to the if statement followed by a further block of code: if (expression) { // code to execute if the expression evaluates to true else { // code to execute in all other cases : An if Statement 2: $mood = happy ; 3: if ($mood == happy ) { 4: echo Hooray! I m in a good mood! ; 5: 6?> When you access this script through your web browser, it produces the following output: Hooray! I m in a good mood! 2 P a g e
If you change the assigned value of $mood to sad or any other string besides happy, and then run the script again, the expression in the if statement evaluates to false, and the code block is skipped. The script remains silent, which leads to the else clause. 3) Using the else Clause with the if Statement When working with an if statement, you might want to define an alternative block of code that should be executed if the expression you are testing evaluates to false. You can do this by adding else to the if statement followed by a further block of code: if (expression) { // code to execute if the expression evaluates to true else { // code to execute in all other cases : An if Statement That Uses else 2: $mood = sad ; 3: if ($mood == happy ) { 4: echo Hooray! I m in a good mood! ; 5: else { 6: echo I m in a $mood mood. ; 7: 8:?> 4) Using the elseif Clause with the if Statement You can use an if...elseif...else clause to test multiple expressions (the if...else portion) before offering a default block of code (the elseif portion): if (expression) { // code to execute if the expression evaluates to true elseif (another expression) { // code to execute if the previous expression failed // and this one evaluates to true else { // code to execute in all other cases An if Statement That Uses else and elseif 2: $mood = sad ; 3: if ($mood == happy ) { 4: echo Hooray! I m in a good mood! ; 3 P a g e
5: elseif ($mood == sad ) { 6: echo Awww. Don t be down! ; 7: else { 8: echo I m neither happy nor sad, but $mood. ; 9: 10:?> 5) The switch Statement The switch statement is an alternative way of changing flow, based on the evaluation of an expression switch (expression) { case result1: // execute this if expression results in result1 break; case result2: // execute this if expression results in result2 break; default: // execute this if no break statement // has been encountered hitherto NOTE: It is important to include a break statement at the end of any code that will be executed as part of a case statement. Without a break statement, the program flow continues to the next case statement and ultimately to the default statement.in most cases, this results in unexpected behavior, likely incorrect! : A switch Statement 2: $mood = sad ; 3: switch ($mood) { 4: case happy : 5: echo Hooray! I m in a good mood! ; 6: break; 7: case sad : 8: echo Awww. Don t be down! ; 9: break; 10: default: 11: echo I m neither happy nor sad, but $mood. ; 12: break; 13: 14:?> 4 P a g e
6) Using the?: Operator The?: or ternary operator is similar to the if statement, except that it returns a value derived from one of two expressions separated by a colon. (expression)? returned_if_expression_is_true : returned_if_expression_is_false; Using the?: Operator 2: $mood = sad ; 3: $text = ($mood == happy )? I am in a good mood! : I am in a $mood mood. ; 4: echo $text ; 5:?> II. Loops 1) The while Statement The while statement looks similar in structure to a basic if statement, but has the ability to loop: while (expression) { // do something A while Statement 2: $counter = 1; 3: while ($counter <= 12) { 4: echo $counter. times 2 is.($counter * 2). <br /> ; 5: $counter++; 6: 7:?> Out put : 1 times 2 is 2 2 times 2 is 4 3 times 2 is 6 4 times 2 is 8 5 times 2 is 10 5 P a g e
6 times 2 is 12 7 times 2 is 14 8 times 2 is 16 9 times 2 is 18 10 times 2 is 20 11 times 2 is 22 12 times 2 is 24 2) The do...while Statement A do...while statement looks a little like a while statement turned on its head. The essential difference between the two is that the code block is executed before the truth test and not after it: do { // code to be executed while (expression); The do...while Statement : 2: $num = 1; 3: do { 4: echo The number is:.$num. <br /> ; 5: $num++; 6: while (($num > 200) && ($num < 400)); 7:?> 3) The for Statement Definition ; for (initialization expression; test expression; modification expression) { // code to be executed NOTE: Infinite loops are, as the name suggests, loops that run without bounds. If yourloop is running infinitely, your script is running for an infinite amount of time. This behavior is very stressful on your web server and renders the web page unusable. Using the for Statement 2: for ($counter=1; $counter<=12; $counter++) { 3: echo $counter. times 2 is.($counter * 2). <br /> ; 4: 6 P a g e
5:?> Output : 1 times 2 is 2 2 times 2 is 4 3 times 2 is 6 4 times 2 is 8 5 times 2 is 10 6 times 2 is 12 7 times 2 is 14 8 times 2 is 16 9 times 2 is 18 10 times 2 is 20 11 times 2 is 22 12 times 2 is 24 III. Breaking Out of Loops with the break Statement Both while and for statements incorporate a built-in test expression with which you can end a loop. However, the break statement enables you to break out of a loop based on the results of additional tests. The following example creates a simple for statement that divides a large number by a variable that is incremented, printing the result to the screen. A for Loop That Divides 4000 by 10 Incremental Numbers 2: for ($counter=1; $counter <= 10; $counter++) { 3: $temp = 4000/$counter; 4: echo 4000 divided by.$counter. is....$temp. <br /> ; 5: 6:?> When you access this script through your web browser, it produces the following output: 4000 divided by 1 is... 4000 4000 divided by 2 is... 2000 4000 divided by 3 is... 1333.33333333 4000 divided by 4 is... 1000 4000 divided by 5 is... 800 4000 divided by 6 is... 666.666666667 4000 divided by 7 is... 571.428571429 4000 divided by 8 is... 500 4000 divided by 9 is... 444.444444444 4000 divided by 10 is... 400 Using the break Statement 2: $counter = -4; 3: for (; $counter <= 10; $counter++) { 7 P a g e
4: if ($counter == 0) { 5: break; 6: else { 7: $temp = 4000/$counter; 8: echo 4000 divided by.$counter. is....$temp. <br /> ; 9: 10: 11?> NOTE: Dividing a number by 0 does not cause a fatal error in PHP. Instead, PHP generates a warning and execution continues. When you access this script through your web browser, it produces the following output: 4000 divided by -4 is... -1000 4000 divided by -3 is... -1333.33333333 4000 divided by -2 is... -2000 4000 divided by -1 is... -4000 1) Skipping an Iteration with the continue Statement The continue statement ends execution of the current iteration but doesn t cause the loop as a whole to end. Instead, the next iteration begins immediately : Using the continue Statement 2: $counter = -4; 3: for (; $counter <= 10; $counter++) { 4: if ($counter == 0) { 5: continue; 6: 7: $temp = 4000/$counter; 8: echo 4000 divided by.$counter. is....$temp. <br /> ; 9: 10:?> When you access this script through your web browser, it produces the following output: 4000 divided by -4 is... -1000 4000 divided by -3 is... -1333.33333333 4000 divided by -2 is... -2000 4000 divided by -1 is... -4000 4000 divided by 1 is... 4000 4000 divided by 2 is... 2000 4000 divided by 3 is... 1333.33333333 4000 divided by 4 is... 1000 4000 divided by 5 is... 800 8 P a g e
4000 divided by 6 is... 666.666666667 4000 divided by 7 is... 571.428571429 4000 divided by 8 is... 500 4000 divided by 9 is... 444.44444444444 4000 divided by 10 is... 400 2) Nesting Loops Loops can contain other loop statements, as long as the logic is valid and the loops are tidy. The combination of such statements proves particularly useful when working with dynamically created HTML tables. Listing 6.12 uses two for statements to print a multiplication table to the browser. Nesting Two for Loops 2: echo <table style=\ border: 1px solid #000;\ > \n ; 3: for ($y=1; $y<=12; $y++) { 4: echo <tr> \n ; 5: for ($x=1; $x<=12; $x++) { 6: echo <td style=\ border: 1px solid #000; width: 25px; 7: text-align:center;\ > ; 8: echo ($x * $y); 9: echo </td> \n ; 10: 11: echo </tr> \n ; 12: 13: echo </table> ; 14:?> 3) Code Blocks and Browser Output Imagine a script that outputs a table of values only when a variable is set to the Boolean value true. The following example shows a simplified HTML table constructed with the code block of an if statement A Code Block Containing Multiple echo Statements 2: $display_prices = true; 3: if ($display_prices) { 4: echo <table border=\ 1\ >\n ; 5: echo <tr><td colspan=\ 3\ > ; 6: echo today s prices in dollars ; 7: echo </td></tr> ; 8: echo <tr><td>\$14.00</td><td>\$32.00</td><td>\$71.00</td></tr>\n ; 9: echo </table> ; 10: 11:?> 9 P a g e
If the value of $display_prices is set to true in line 2, the table is printed. For the sake of readability, we split the output into multiple echo() statements, and once again use the backslash to escape any quotation marks used in the HTML output IV. Summary In this chapter, you learned about control structures and the ways in which they can help to make your scripts flexible and dynamic. Most of these structures reappear regularly throughout the rest of the book. You learned how to define an if statement and how to provide for alternative actions with the elseif and else clauses. You learned how to use the switch statement to change flow according to multiple equivalence tests on the result of an expression. You learned about loops in particular, the while and for statements and you learned how to use break and continue to prematurely end the execution of a loop or to skip an iteration. You learned how to nest one loop within another and saw a typical use for this structure. You also looked at a technique for using PHP start and end tags in conjunction with conditional code blocks, to alleviate having to escape (use the backslash in front of) special characters such as the quotation mark and dollar sign. 10 P a g e