1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P26 potx

5 253 0
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 143,08 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Repeating Code with a while Loop The code for the while.phpprogram is much like the forloop example, but you can see that the whileloop is a little bit simpler: A simple While Loop 1

Trang 1

<h1>Counting Backwards</h1>

<?

for ($i = 10; $i > 0; $i—){

print “$i <br>\n”;

} // end for loop

?>

</body>

</html>

If you understand how forloops work, the changes all make sense I’m counting

backwards this time, so $ibegins 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 $iis greater than 0 The loop ends as soon as $iis 0or 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 forloop You can use the whileloop when you know how many

times something will happen Figure 4.6 shows how a whileloop can work much

like a forloop

Repeating Code with a while Loop

The code for the while.phpprogram is much like the forloop example, but you

can see that the whileloop is a little bit simpler:

<html>

<head>

<title>

A simple While Loop

</title>

103

Trang 2

<body>

<h1>A simple while loop</h1>

<?

$i = 1;

while ($i <= 10){

print “$i <br>\n”;

$i++;

} // end while

?>

</body>

</html>

The whileloop 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

l u

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.

Trang 3

This particular program starts by initializing the variable $i, then checking to

see if it’s greater than or equal to 10in the whilestatement Inside the loop body,

the program prints the current value of $iand increments $i

Recognizing Endless Loops

The flexibility of the whileconstruct 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

Trang 4

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 $jhas 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 forloop’s structure When you write a whileloop, 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

T R I C K

T R A P

106

l u

Trang 5

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

forconstruct

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 forloop

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

FIGURE 4.7

The information

displayed on this

page is stored in

two array variables.

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