PHP 5/MySQL Programming- P26 potx

5 252 0
PHP 5/MySQL Programming- P26 potx

Đang tải... (xem toàn văn)

Thông tin tài liệu

<h1>Counting Backwards</h1> <? for ($i = 10; $i > 0; $i—){ print “$i <br>\n”; } // end for loop ?> </body> </html> If you understand how for loops work, the changes all make sense. I’m counting backwards this time, so $i begins with a large value (in this case 10). The condi- tion for continuing the loop is now $i > 0, which means the loop continues as long as $i is greater than 0. The loop ends as soon as $i is 0 or less. Note that rather than adding a value to $i, this time I decrement by 1 each time through the loop. If you’re counting backwards, be very careful that the sentry variable has a mechanism for getting smaller. Otherwise the loop never ends. Recall that $i++ adds 1 to $i; $i— subtracts 1 from $i. Using a while Loop PHP, like most languages, provides another kind of looping structure even more flexible than the for loop. You can use the while loop when you know how many times something will happen. Figure 4.6 shows how a while loop can work much like a for loop. Repeating Code with a while Loop The code for the while.php program is much like the for loop example, but you can see that the while loop is a little bit simpler: <html> <head> <title> A simple While Loop </title> 103 C h a p t e r 4 L o o p s a n d A r r a y s </head> <body> <h1>A simple while loop</h1> <? $i = 1; while ($i <= 10){ print “$i <br>\n”; $i++; } // end while ?> </body> </html> The while loop requires only one parameter, which is a condition. The loop con- tinues as long as the condition is evaluated as TRUE. As soon as the condition is evaluated as FALSE, the loop exits. 104 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 4.6 Although this program’s output looks a lot like the basic for loop, it uses a different construct to achieve the same result. This particular program starts by initializing the variable $i, then checking to see if it’s greater than or equal to 10 in the while statement. Inside the loop body, the program prints the current value of $i and increments $i. Recognizing Endless Loops The flexibility of the while construct gives it power, but with that power comes potential for problems. while loops are easy to build, but a loop that works improperly can cause a lot of trouble. It’s possible that the code in the loop will never execute at all. Even worse, you might have some sort of logical error that causes the loop to continue indefinitely. As an example, look at the badWhile.php code: <html> <head> <title> A bad While Loop </title> </head> <body> <h1>A bad while loop</h1> <? $i = 1; while ($i <= 10){ print “$i <br>\n”; $j++; } // end while ?> </body> </html> 105 C h a p t e r 4 L o o p s a n d A r r a y s The badWhile.php program shows what happens when you have an endless loop in your code. If you run this program, it may temporarily slow down your Web server. Be sure your server is configured to stop a PHP process when the user presses the stop button on the browser. (This is a default setting on most PHP installations.) The badWhile.php program has a subtle but deadly error. Look carefully at the source code and see if you can spot it. The code is just like the first while pro- gram, except instead of incrementing $i, I incremented $j. The variable $j has nothing to do with $i and $i never changes. The loop keeps going on forever, because it cannot end until $i is greater than or equal to 10, which never hap- pens. This program is an example of the classic endless loop. Every programmer alive has written them accidentally, and you will too. Usually the culprit of an endless loop is a sloppy variable name, spelling, or capitalization. If you use a variable like $myCounter as the sentry variable but then increment $MyCounter, PHP tracks two entirely different variables. Your program won’t work correctly. This is another reason to be consistent on your variable naming and capitalization conventions. Building a Well-Behaved Loop Fortunately, you have guidelines for building a loop that behaves as you wish. Even better, you’ve already learned most of the important ideas, because these fundamental concepts are built into the for loop’s structure. When you write a while loop, you are responsible for these three things: • Creating a sentry variable • Building a condition • Ensuring the loop can exit I discuss each of these ideas in the following sections. Creating and Initializing a Sentry Variable If your loop is based on a variable’s value (there are alternatives), make sure you do these three things: • Identify that variable. • Ensure that variable has appropriate scope. • Make sure that variable has a reasonable starting value. TRICK TRAP 106 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r You might also check that value to ensure the loop runs at least one time (at least if that’s your intent). Creating a variable is much like the initialization stage of a for construct. Building a Condition to Continue the Loop Your condition usually compares a variable and a value. Make sure you have a condition that can be met and be broken. The hard part is ensuring that the pro- gram gets out of the loop at the correct time. This condition is much like the con- dition in the for loop. Ensuring the Loop Can Exit There must be some trigger that changes the sentry variable so the loop can exit. This code must exist inside the code body. Be sure it is possible for the sentry vari- able to achieve the value necessary to exit the loop by making the condition false. Working with Basic Arrays Programming is about the combination of control structures (like loops) and data structures (like variables). You know the very powerful looping structures. Now it’s time to look at a data structure that works naturally with loops. Arrays are special variables made to hold lists of information. PHP makes it quite easy to work with arrays. Look at Figure 4.7, whose basicArray.php program demonstrates two arrays. 107 C h a p t e r 4 L o o p s a n d A r r a y s FIGURE 4.7 The information displayed on this page is stored in two array variables. . badWhile .php program shows what happens when you have an endless loop in your code. If you run this program, it may temporarily slow down your Web server. Be sure your server is configured to stop a PHP. user presses the stop button on the browser. (This is a default setting on most PHP installations.) The badWhile .php program has a subtle but deadly error. Look carefully at the source code and. Arrays are special variables made to hold lists of information. PHP makes it quite easy to work with arrays. Look at Figure 4.7, whose basicArray .php program demonstrates two arrays. 107 C h a p t e r

Ngày đăng: 07/07/2014, 02:20

Mục lục

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

    • Contents

    • Introduction

    • Chapter 1: Exploring the PHP Environment

    • Chapter 2: Using Variables and Input

    • Chapter 3: Controlling Your Code with Conditions and Functions

    • Chapter 4: Loops and Arrays

    • Chapter 5: Better Arrays and String Handling

    • Chapter 6: Working with Files

    • Chapter 7: Writing Programs with Objects

    • Chapter 8: XML and Content Management Systems

    • Chapter 9: Using MySQL to Create Databases

    • Chapter 10: Connecting to Databases within PHP

    • Chapter 11: Data Normalization

    • Chapter 12: Building a Three-Tiered Data Application

    • Index

    • Team DDU

Tài liệu cùng người dùng

Tài liệu liên quan