PHP 5/MySQL Programming- P25 pot

5 248 0
PHP 5/MySQL Programming- P25 pot

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

Thông tin tài liệu

<head> <title> A simple For Loop </title> </head> <body> <h1>A simple for loop</h1> <? for ($i = 0; $i < 10; $i++){ print “$i <br>\n”; } // end for loop ?> </body> </html> Each number is printed in the line that looks like this: print “$i <br>\n”; This line can print only one value, but it happens 10 times. The key to this behav- ior is the for statement. The for structure has three main parts: a variable decla- ration, a condition, and an increment statement. The \n character signifies a newline or carriage return. This means that the program’s HTML source code places each number on a separate line. The <br> tag ensures that the HTML output also places each number on its own line. While carriage returns in the HTML source don’t have much to do with how the output looks, I like my programs’ code to be written as carefully as the stuff I build by hand. Initializing a Sentry Variable for loops usually involve an integer (non-decimal) variable. Sometimes the key vari- able in a loop is referred to as a sentry variable, because it acts like a gatekeeper to the loop. The first part of a for loop definition is a line of code that identifies and initializes the sentry variable to some starting value. In the simple for loop demo, the initialization segment looks like this: TRICK 98 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 99 C h a p t e r 4 L o o p s a n d A r r a y s $i = 0; It specifies that the sentry variable be called $i and its starting value be 0. Computer programs frequently begin counting with zero, so I initialized $i to 0 as well. Although the $i = 0; segment looks like (and is) a complete line of code, it is usually placed on the same line as the other parts of the for loop construct. Setting a Condition to Finish the Loop Getting a computer to repeat behavior is the easy part. The harder task comes when trying to get the computer to stop correctly. The second part of the for loop construct is a condition. When this condition is evaluated as TRUE, the loop should continue. The loop should exit as soon as the condition is evaluated to FALSE. In this case, I set the condition as $i < 10. This means that as long as the variable $i has a value less than 10, the loop continues. As soon as the program detects that $i has a value equal to or larger than 10, the loop exits. Usually a for loop’s con- dition checks the sentry variable against some terminal or ending value. Changing the Sentry Variable The final critical element of a for loop is some mechanism for changing the value of the sentry variable. At some point the value of $i must become 10 or TRICK IN THE REAL WORLD You might wonder why the sentry variable is called $i. Like most variables, it’s best if sentry variables have a name that suits their purpose. Sometimes, however, a for loop sentry is simply an integer and doesn’t have any other mean- ing. In those situations, an old programming tradition is often called into play. In the Fortran language (one of the earliest common programming languages), all integer variables had to begin with the letters i, j, and a few other characters. Fortran programmers would commonly use i as the name of generic sentry variables. Even though most modern programmers have never written a line of Fortran code, the tradition remains. It’s amazing how much folklore exists in such a relatively new activity as computer programming. 100 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 larger or the loop continues forever. In the basicLoop program, the part of the for structure that makes this happen looks like $i++. The notation $i++ is just like saying add one to $i or $i = $i + 1. The ++ symbol is called an increment opera- tor because it provides an easy way to increment (add 1) to a variable. Building the Loop Once you’ve set up the parts of the for statement, the loop itself is easy to use. Place braces ( {}) around your code and indent all code that’s inside the loop. You can have as many lines of code as you wish inside a loop, including branching statements and other loops. The sentry variable has special behavior inside the loop. It begins with the initial value. Each time the loop repeats, it is changed as specified in the for structure, and the interpreter checks the condition to ensure that it’s still true. If so, the code in the loop occurs again. In the case of the basicArray program, $i begins as 0. The first time the print statement occurs, it prints 0 because that is the current value of $i. When the interpreter reaches the right brace that ends the loop, it increments $i by 1 (fol- lowing the $i++ directive in the for structure) and checks the condition ($i < 10). Because 0 is less than 10, the condition is true and the code inside the loop occurs again. Eventually, the value of $i becomes 10, so the condition ($i < 10) is no longer true. Program control then reverts to the next line of code after the end of the loop, which ends the program. Modifying the for Loop Once you understand for loop structure basics, you can modify it in a couple of interesting ways. You can build a loop that counts by fives or that counts backwards. Counting by Fives The countByFive.php program shown in Figure 4.4 illustrates a program that counts by fives. The program is very much like the basicArray program, but with a couple of twists. <html> <head> <title> Counting By Fives </title> </head> <body> <h1>Counting By Fives</h1> <? for ($i = 5; $i <= 50; $i+= 5){ print “$i <br>\n”; } // end for loop ?> </body> </html> The only thing I changed was the various parameters in the for statement. Since it seems silly to start counting at 0, I set the initial value of $i to 5. I decided to stop when $i reached 50 (after 10 iterations). Each time through the loop, $i is incremented by five. 101 C h a p t e r 4 L o o p s a n d A r r a y s FIGURE 4.4 This program uses a for loop to count by five. 102 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 The += syntax in the following code increments a variable: $i += 5; The above is the same thing as this: $i = $i + 5; Counting Backwards It is fairly simple to modify a for loop so it counts backwards. Figure 4.5 illus- trates this feat. Once again, the basic structure is just like the basic for loop program, but chang- ing the for structure parameters alters the program’s behavior. The code for this program shows how it is done: <html> <head> <title> Counting Backwards </title> </head> <body> FIGURE 4.5 This program counts backwards from 10 to 1 using a for loop. . can build a loop that counts by fives or that counts backwards. Counting by Fives The countByFive .php program shown in Figure 4.4 illustrates a program that counts by fives. The program is very

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

  • Đang cập nhật ...

Tài liệu liên quan