Counting Backwards If you understand how forloops work,

September 30, 2006 on 8:39 am | In php | No Comments

Generating a Basic ArrayLook at the lines that describe $camelPop: //simply assign values to array$camelPop[1] = Somalia ; $camelPop[2] = Sudan ; $camelPop[3] = Mauritania ; $camelPop[4] = Pakistan ; $camelPop[5] = India ; The $camelPopvariable is a variable meant to hold the five countries with thelargest camel populations in the world. (If this array stuff isn t working for you, at least you ve learned something in this chapter!) Since $camelPopis going tohold the names of five different countries, it makes sense that this is an array(computer geek lingo for list) rather than an ordinary variable. The only thing different about $camelPopand all the other variables you veworked with so far is $camelPopcan have multiple values. To tell these valuesapart, use a numeric index in square brackets. Apparently the boxer George Foreman has several sons also named George. I veoften wondered what Mrs. Foreman does when she wants somebody to take outthe trash. I suspect she has assigned a number to each George, so there is noambiguity. This is exactly how arrays work. Each element has the same name, buta different numerical index so you can tell them apart. Many languages require you to explicitly create array variables, but PHP is veryeasygoing in this regard. Simply assign a value to a variable with an index insquare brackets and you ve created an array. Even though PHP is good natured about letting you create an array variable on- the-fly, you might get a warning about this behavior on those Web servers thathave error reporting set to E_ALL. If that s the case, you can create an empty arraywith the array()function described in the following sections and then add values to it. Using a Loop to Examine an Array s ContentsArrays go naturally with forloops. Very often when you have an array variable, you step through all of its values and do something to each one. In this example, I want to print the index and the corresponding country s name. Here s the forloop that performs this task: TRICKTRICK109Chapter
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Counting Backwards If you understand how forloops work,

September 30, 2006 on 8:39 am | In php | No Comments

First look over the entire program, then see how it does its work.

Basic Array

Top Camel Populations in the World

\n ; for ($i = 1; $i <= 5; $i++){ print $i: $camelPop[$i]
\n ; } // end for loopprint Source: Food and Agriculture Organization of the United Nations\n ; //use array function to load up array$binary = array( 000 , 001 , 010 , 011 ); print

Binary numbers

\n ; for ($i = 0; $i < count($binary); $i++){ print $i: $binary[$i]
\n ; } // end for loop?> 108PHP5/MySQLProgrammingfortheAbsoluteBeginner
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Counting Backwards If you understand how forloops work,

September 30, 2006 on 8:39 am | In php | No Comments

You might also check that value to ensure the loop runs at least one time (at leastif that s your intent). Creating a variable is much like the initialization stage of aforconstruct. Building a Condition to Continue the LoopYour condition usually compares a variable and a value. Make sure you have acondition 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 ExitThere 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 ArraysProgramming is about the combination of control structures (like loops) anddata 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. Arraysare special variables made to hold lists of information. PHP makes it quiteeasy to work with arrays. Look at Figure 4.7, whose basicArray.phpprogramdemonstrates two arrays. LoopsandArraysFIGURE 4.7The informationdisplayed on thispage is stored intwo array variables.
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Counting Backwards If you understand how forloops work,

September 30, 2006 on 8:39 am | In php | No Comments

ThebadWhile.phpprogram shows what happens when you have an endlessloop in your code. If you run this program, it may temporarily slow down your Webserver. Be sure your server is configured to stop a PHP process when the userpresses the stopbutton on the browser. (This is a default setting on most PHPinstallations.) The badWhile.phpprogram has a subtle but deadly error. Look carefully at thesource code and see if you can spot it. The code is just like the first whilepro- gram, except instead of incrementing $i, I incremented $j. The variable $jhasnothing to do with $iand $inever changes. The loop keeps going on forever, because it cannot end until $iis greater than or equal to 10, which never hap- pens. This program is an example of the classic endless loop.Every programmeralive 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 $myCounteras the sentry variable but then increment $MyCounter, PHP tracks two entirely different variables. Yourprogram won t work correctly. This is another reason to be consistent on your variable naming and capitalization conventions. Building a Well-Behaved LoopFortunately, you have guidelines for building a loop that behaves as you wish. Even better, you ve already learned most of the important ideas, because thesefundamental concepts are built into the forloop s structure. When you write awhileloop, you are responsible for these three things: Creating a sentry variable Building a condition Ensuring the loop can exitI discuss each of these ideas in the following sections. Creating and Initializing a Sentry VariableIf your loop is based on a variable s value (there are alternatives), make sure youdo these three things: Identify that variable. Ensure that variable has appropriate scope. Make sure that variable has a reasonable starting value. TRICKTRAP106PHP5/MySQLProgrammingfortheAbsoluteBeginner
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Counting Backwards If you understand how forloops work,

September 30, 2006 on 8:39 am | In php | No Comments

This particular program starts by initializing the variable $i, then checking tosee 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 LoopsThe flexibility of the whileconstruct gives it power, but with that power comespotential for problems. whileloops are easy to build, but a loop that worksimproperly can cause a lot of trouble. It s possible that the code in the loop willnever execute at all. Even worse, you might have some sort of logical error thatcauses the loop to continue indefinitely. As an example, look at the badWhile.phpcode:

A bad while loop

\n ; $j++; } // end while ?>
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Counting Backwards If you understand how forloops work,

September 30, 2006 on 8:39 am | In php | No Comments


A simple while loop

\n ; $i++; } // end while ?> 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 isevaluated as FALSE, the loop exits. 104PHP5/MySQLProgrammingfortheAbsoluteBeginnerFIGURE 4.6Although thisprogram s outputlooks a lot like thebasicforloop, it uses a differentconstruct toachieve the same result.
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Counting Backwards If you understand how forloops work,

September 30, 2006 on 8:39 am | In php | No Comments

Counting Backwards

0; $i ){ print $i
\n ; } // end for loop?> If you understand how forloops work, the changes all make sense. I m countingbackwards 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 aslong 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 timethrough the loop. If you re counting backwards, be very careful that the sentryvariable 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 LoopPHP, like most languages, provides another kind of looping structure even moreflexible than the forloop. You can use the whileloop when you know how manytimes something will happen. Figure 4.6 shows how a whileloop can work muchlike a forloop. Repeating Code with a while LoopThe code for the while.phpprogram is much like the forloop example, but youcan see that the whileloop is a little bit simpler:
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

100PHP5/MySQLProgrammingfortheAbsoluteBeginnerlarger or the loop continues forever. In the

September 30, 2006 on 3:30 am | In php | No Comments

102PHP5/MySQLProgrammingfortheAbsoluteBeginnerThe +=syntax in the following code increments a variable: $i += 5; The above is the same thing as this: $i = $i + 5; Counting BackwardsIt is fairly simple to modify a forloop so it counts backwards. Figure 4.5 illus- trates this feat. Once again, the basic structure is just like the basic forloop program, but chang- ing the forstructure parameters alters the program s behavior. The code for thisprogram shows how it is done: FIGURE 4.5This programcounts backwardsfrom 10 to 1 using aforloop.
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services

100PHP5/MySQLProgrammingfortheAbsoluteBeginnerlarger or the loop continues forever. In the

September 30, 2006 on 3:30 am | In php | No Comments

100PHP5/MySQLProgrammingfortheAbsoluteBeginnerlarger or the loop continues forever. In the basicLoopprogram, the part of the forstructure that makes this happen looks like $i++. The notation $i++is just likesaying add one to $ior $i = $i + 1. The ++symbol is called an increment opera- torbecause it provides an easy way to increment (add 1) to a variable. Building the LoopOnce you ve set up the parts of the forstatement, the loop itself is easy to use. Place braces ({}) around your code and indent all code that s inside the loop. Youcan have as many lines of code as you wish inside a loop, including branchingstatements and other loops. The sentry variable has special behavior inside the loop. It begins with the initialvalue. Each time the loop repeats, it is changed as specified in the forstructure, and the interpreter checks the condition to ensure that it s still true. If so, thecode in the loop occurs again. In the case of the basicArrayprogram, $ibegins as 0. The first time the printstatement occurs, it prints 0because that is the current value of $i.When theinterpreter reaches the right brace that ends the loop, it increments $iby 1 (fol- lowing the $i++directive in the forstructure) and checks the condition ($i < 10). Because 0is less than 10, the condition is true and the code inside the loop occursagain. Eventually, the value of $ibecomes 10, so the condition ($i < 10) is nolonger true. Program control then reverts to the next line of code after the endof the loop, which ends the program. Modifying the for LoopOnce you understand forloop structure basics, you can modify it in a couple ofinteresting ways. You can build a loop that counts by fives or that counts backwards. Counting by FivesThe countByFive.phpprogram shown in Figure 4.4 illustrates a program thatcounts by fives. The program is very much like the basicArrayprogram, but with a couple of twists. <br /> Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra <a target="_blank" href="http://www.astrawebhosting.net">servlet hosting</a> services </p> <div class="feedback"></div> <!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://www.sandzak.com/blog/2006/09/30/100php5mysqlprogrammingfortheabsolutebeginnerlarger-or-the-loop-continues-forever-in-the/" dc:identifier="http://www.sandzak.com/blog/2006/09/30/100php5mysqlprogrammingfortheabsolutebeginnerlarger-or-the-loop-continues-forever-in-the/" dc:title="100PHP5/MySQLProgrammingfortheAbsoluteBeginnerlarger or the loop continues forever. In the" trackback:ping="http://www.sandzak.com/blog/2006/09/30/100php5mysqlprogrammingfortheabsolutebeginnerlarger-or-the-loop-continues-forever-in-the/trackback/" /> </rdf:RDF> --> </div> <div class="entrada"> <h2 id="post-182"><a href="http://www.sandzak.com/blog/2006/09/30/100php5mysqlprogrammingfortheabsolutebeginnerlarger-or-the-loop-continues-forever-in-the-2/" rel="bookmark" title="Permanent Link to 100PHP5/MySQLProgrammingfortheAbsoluteBeginnerlarger or the loop continues forever. In the">100PHP5/MySQLProgrammingfortheAbsoluteBeginnerlarger or the loop continues forever. In the</a></h2> <small>September 30, 2006 on 3:30 am | In <a href="http://www.sandzak.com/blog/category/php/" title="View all posts in php" rel="category tag">php</a> | <a href="http://www.sandzak.com/blog/2006/09/30/100php5mysqlprogrammingfortheabsolutebeginnerlarger-or-the-loop-continues-forever-in-the-2/#respond" title="Comment on 100PHP5/MySQLProgrammingfortheAbsoluteBeginnerlarger or the loop continues forever. In the">No Comments</a></small> <p>Counting By Fives

Counting By Fives

\n ; } // end for loop?> The only thing I changed was the various parameters in the forstatement. Sinceit seems silly to start counting at 0, I set the initial value of $ito 5. I decided tostop when $ireached 50(after 10 iterations). Each time through the loop, $iisincremented by five. LoopsandArraysFIGURE 4.4This program usesaforloop to count by five.
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services

Next Page »

Powered by cheap hosting