Counting Backwards If you understand how forloops work,
September 30, 2006 on 8:39 am | In php | No CommentsGenerating 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 CommentsFirst look over the entire program, then see how it does its work.
Basic Array
//simply assign values to array$camelPop[1] = Somalia ; $camelPop[2] = Sudan ; $camelPop[3] = Mauritania ; $camelPop[4] = Pakistan ; $camelPop[5] = India ; //output array valuesprint
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 CommentsYou 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 CommentsThebadWhile.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 CommentsThis 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
$i = 1; while ($i <= 10){ print $i
\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
$i = 1; while ($i <= 10){ print $i
\n ; $i++; } // end while ?>