While the output of the simpleFor.phpprogram doesn t look

September 29, 2006 on 11:35 pm | In php | No Comments


A simple for loop

\n ; } // end for loop?> Each number is printed in the line that looks like this: print $i
\n ; This line can print only one value, but it happens 10 times. The key to this behav- ior is the forstatement. The forstructure has three main parts: a variable decla- ration, a condition, and an increment statement. The\ncharacter signifies a newlineor carriage return. This means that the program sHTML source code places each number on a separate line. The
tag ensuresthat the HTML output also places each number on its own line. While carriagereturns 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 Variableforloops usually involve an integer (non-decimal) variable. Sometimes the key vari- able in a loop is referred to as a sentryvariable,because it acts like a gatekeeper tothe loop. The first part of a forloop definition is a line of code that identifies andinitializes the sentry variable to some starting value. In the simple forloop demo, the initialization segment looks like this: TRICK98PHP5/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

While the output of the simpleFor.phpprogram doesn t look

September 29, 2006 on 11:35 pm | In php | No Comments

LoopsandArrays$i = 0; It specifies that the sentry variable be called $iand its starting value be 0. Computer programs frequently begin counting with zero, so I initialized $ito 0as well. Although the $i = 0;segment looks like (and is) a complete line of code, it isusually placed on the same line as the other parts of the forloop construct. Setting a Condition to Finish the LoopGetting a computer to repeat behavior is the easy part. The harder task comeswhen trying to get the computer to stop correctly. The second part of the forloopconstruct is a condition. When this condition is evaluated as TRUE, the loop shouldcontinue. The loop should exit as soon as the condition is evaluated to FALSE. Inthis case, I set the condition as $i < 10. This means that as long as the variable $ihas a value less than 10, the loop continues. As soon as the program detects that$ihas a value equal to or larger than 10, the loop exits. Usually a forloop s con- dition checks the sentry variable against some terminal or ending value. Changing the Sentry VariableThe final critical element of a forloop is some mechanism for changing thevalue of the sentry variable. At some point the value of $imust become 10orTRICKINTHEREALWORLDYou 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 forloop 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 ias the name of generic sentry variables. Even though most modern programmers have never written a line ofFortran code, the tradition remains. It s amazing how much folklore exists insuch a relatively new activity as computer programming.
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

While the output of the simpleFor.phpprogram doesn t look

September 29, 2006 on 11:35 pm | In php | No Comments

While the output of the simpleFor.phpprogram doesn t look all that interesting, it has a unique characteristic. It has only one printstatement in the entire pro- gram, which is executed 10 different times. Take a look at the source code to seehow it works: LoopsandArraysFIGURE 4.2The player hasearned back somemoney with a fullhouse! FIGURE 4.3This programcounts from zero toone using only oneprintstatement.
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

94PHP5/MySQLProgrammingfortheAbsoluteBeginnerThe Ending HTML CodeThe final set of HTML

September 29, 2006 on 7:18 pm | In php | No Comments

Introducing the Poker Dice ProgramThe main program for this chapter is a simplified dice game. In this game, youare given $100 of virtual money. On each turn, you bet two dollars. The computerrolls five dice. You can elect to keep each die or roll it again. On the second roll, the computer checks for various combinations. You can earn money back forrolling pairs, triples, four or five of a kind, and straights (five numbers in a row). Figures 4.1 and 4.2 illustrate the game in action. The basic concepts of this game are much like the ones you use in other chapters programs. Keeping track of all five dice can get complicated, so this program usesarrays and loops to manage all the information. Counting with the for LoopYou might want the computer to repeat some sort of action multiple times. Good thing computers excel at repetitive behavior. For example, take a look atthe simpleFor.phpprogram shown in Figure 4.3.96PHP5/MySQLProgrammingfortheAbsoluteBeginnerFIGURE 4.1After the first roll, you can keep someof the dice byselecting thecheckboxesunderneatheach die.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

94PHP5/MySQLProgrammingfortheAbsoluteBeginnerThe Ending HTML CodeThe final set of HTML

September 29, 2006 on 7:18 pm | In php | No Comments

LoopsandArrays4CHAPTERYou know all a program s basic parts, but your programs can be much easierto write and more efficient when you know some other things. In this chapteryou learn about two very important tools, arrays and looping structures. Arraysare special variables that form lists. Loopingstructuresrepeat certain codesegments. As you might expect, arrays and loops often work together. You learn howto use these new elements to make more interesting programs. Specifically, you dothese things: Use the forloop to build basic counting structures Modify the forloop for different kinds of counting Use a whileloop for more flexible looping Identify the keys to successful loops Create basic arrays Write programs that use arrays and loops Store information in hidden fields
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

94PHP5/MySQLProgrammingfortheAbsoluteBeginnerThe Ending HTML CodeThe final set of HTML

September 29, 2006 on 7:18 pm | In php | No Comments

94PHP5/MySQLProgrammingfortheAbsoluteBeginnerThe Ending HTML CodeThe final set of HTML code closes everything up. It completes the PHP segment, the font, the centered text, the body, and finally, the HTML itself. ?> SummaryYou learn a lot in this chapter. You learned several kinds of branching structures, including the ifclause, elsestatements, and the switchstructure. You know howto write functions, which make your programs much more efficient and easierto read. You know how to pass parameters to functions and return values fromthem. You can access global variables from inside functions. You put all thesethings together to make an interesting game. You should be very proud! In thenext chapter you learn how to use looping structures to make your programseven more powerful. CHALLENGES1.Write a program that generates 4-, 10-, or 20-sided dice. 2.Write a program that lets the user choose how many sides a die has andprint a random roll with the appropriate maximum values. (Don t worryabout using images to display the dice.) 3.Write a Loaded Diceprogram that half the time generates the value 1andhalf the time generates some other value. 4.Modify the story game from chapter 2, Using Variables and Input, so theform and the program are one file. 5.Create a Web page generator. Make a form for the page caption, backgroundcolor, font color, and text body. Use this form to generate an HTML page.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

ControllingYourCodewithConditionsandFunctions$die1 = rand(1,6); $die2 = rand(1,6); $die3 =

September 29, 2006 on 3:27 pm | In php | No Comments

The first style is much shorter and easier to type, so it s the form most programmersprefer. The printForm() FunctionThe purpose of the printForm()function is to print the form at the bottom of theHTML page. This form is pretty straightforward except for the need to place thehidden field for $numPetals. function printForm(){ global $numPetals; print <<

How many petals around the rose?


give me a hint HERE; } // end printFormThis code places the form on the page. I could have done most of the form in plainHTML without needing PHP for anything but the hidden field. However, when Istart using PHP, I like to have much of my code in PHP. It helps me see the flow ofthings more clearly (print greeting, print dice, and print form, for example).
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

ControllingYourCodewithConditionsandFunctions$die1 = rand(1,6); $die2 = rand(1,6); $die3 =

September 29, 2006 on 3:27 pm | In php | No Comments

width = 100> HERE; } // end showDieOne advantage of using functions for repetitive HTML code is the ease with whichyou can modify large sections of code. For example, if you wish to change imagesizes, change the imgtag in this one function. All six die images are changed. The calcNumPetals FunctionThe printDice()function also calls calcNumPetals()once for each die. This func- tion receives a die value as a parameter. It also references the $numPetalsglobalvariable. The function uses a switchstatement to determine how much to add to$numPetalsbased on the current die s value. Here s the trick: The center dot of the die is the rose. Any dots around the centerdot are the petals. The value 1has a rose but no petals; 2, 4, and 6have petals, butno rose; 3has two petals; 5has four. If the die roll is 3, $numPetalsshould beincreased by 2; if the roll is 5, $numPetalsshould be increased by 4. function calcNumPetals($value){ global $numPetals; switch ($value) { case 3: $numPetals +=2; break; case 5: $numPetals +=4; break; } // end switch} // end calcNumPetalsThe +=code is a shorthand notation. The line shown here$numPetals +=2; is exactly equivalent to this line: $numPetals = $numPetals + 2; TRICK92PHP5/MySQLProgrammingfortheAbsoluteBeginner
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

ControllingYourCodewithConditionsandFunctions$die1 = rand(1,6); $die2 = rand(1,6); $die3 =

September 29, 2006 on 3:27 pm | In php | No Comments

ControllingYourCodewithConditionsandFunctions$die1 = rand(1,6); $die2 = rand(1,6); $die3 = rand(1,6); $die4 = rand(1,6); $die5 = rand(1,6); showDie($die1); showDie($die2); showDie($die3); showDie($die4); showDie($die5); print
; calcNumPetals($die1); calcNumPetals($die2); calcNumPetals($die3); calcNumPetals($die4); calcNumPetals($die5); } // end printDiceThe printDice()function is very concerned with the $numPetalsvariable, butdoesn t need access to $guess. It requests access to $numPetalsfrom the main pro- gram. After printing out the New Roll message, it resets $numPetalsto 0. Thevalue of $numPetalsis recalculated each time the dice are rolled. I got new dice values by calling the rand(1, 6)function six times. I stored eachresult in a different variable, named $die1to $die6. To print out an appropriategraphic for each die, I called the showDie()function. I printed out a line break, then called the calcNumPetals()function once for each die. The showDie() FunctionThe showDie()function is used to simplify repetitive code. It accepts a die valueas a parameter and generates the appropriate HTML code for drawing a die withthe corresponding number of dots. function showDie($value){ print << Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

Returning to the Petals GameAt the beginning of

September 29, 2006 on 11:20 am | In php | No Comments

} else if ($guess == $numPetals){ print

You Got It!

; } else { print <<

from last try:

you guessed: $guess

-and the correct answer was: $numPetals petals around the rose
HERE; } // end if} // end printGreetingThis function refers to both the $guessand $numPetalsvariables, which are auto- matically created. You can use one globalstatement to make more than one vari- able global by separating the variables with commas. The $guessvariable is empty if this is the first time the user has come to the pro- gram. If $guessis empty, I print a welcoming greeting. The user has guessed cor- rectly if $guessis equal to $numPetals, so I print an appropriate congratulations. Ifneither of these conditions is true (which is most of the time), the function printsout a slightly more complex string indicating the user s last guess and the correctanswer. This should give the user enough information to finally solve the riddle. The else ifstructure turns out to be the easiest option for handling the threepossible conditions I want to check. The printDice() FunctionAfter the program prints a greeting, it does the important business of generatingthe random dice. It s relatively easy to generate random dice, as you saw earlierin this chapter. However, I also wanted to be efficient and calculate the correctnumber of petals. To make the printDice()function more efficient, it calls someother custom functions. function printDice(){ global $numPetals; print

New Roll:

; $numPetals = 0; 90PHP5/MySQLProgrammingfortheAbsoluteBeginner
Note: If you are looking for reliable and quality webspace company to host and run your servlet application check Actions servlet hosting services

« Previous PageNext Page »

Powered by cheap hosting