58 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 rand function generates a random number between 1 and 6 (inclusive) and stores the resulting value in the $roll variable. The rand function expects two parameters: The first value is the lowest number and the second value represents the highest number. Since I want to replicate an ordinary six-sided die, I told the rand function to return a value between 1 and 6. Since I knew that rand would return a value, I assigned that resulting value to the variable $roll. By the time the following line has finished executing, the $roll variable has a random value in it: $roll = rand(1,6); The lowest possible value is 1, the highest possible value is 6, and the value will not have a decimal part. (In other words, it will never be 1.5.) If you’re coming from another programming language, you might be surprised at the way random numbers are generated in PHP. Most languages allow you to create a random floating-point value between 0 and 1, and then requires you to transform that value to whatever range you wish. PHP allows—in fact, requires—you to create random integers within a range, which is usually what you want anyway. If you really want a value between 0 and 1, you can generate a random number between 0 and 1000 and then divide that value by 1000. Printing a Corresponding Image Notice the sneaky way I used variable interpolation in the preceding code. I care- fully named my first image die1.jpg, the second die2.jpg, and so on. When I was ready to print an image to the screen, I used an ordinary HTML image tag with the source set to die$roll.jpg. If $roll is 3, the image shows die3.jpg. Variable interpolation can be a wonderful trick if you know how the filenames are structured. You might recall from chapter 2, “Using Variables and Input,” that interpolation is the technique that allows you to embed a variable in a quoted string by simply using its name. Using the if Statement to Control Program Flow One of the most interesting things computers do is appear to make decisions. The decision-making ability is an illusion. The programmer stores very specific instructions inside a computer, and it acts on those instructions. The simplest form of this behavior is a structure called the if statement. TRICK Introducing the Ace Program You can improve the Roll Em program with an if structure. Enter the Ace pro- gram. Figure 3.3 shows the program when the program rolls any value except 1. However, this program does something exciting (okay, moderately exciting), when it rolls a 1, as you can see from Figure 3.4. 59 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s FIGURE 3.3 When the roll is not a 1, nothing interesting happens. IN THE REAL WORLD The dice games in this chapter demonstrate the power of graphical images to make your programs more interesting and fun. You can get graphics for your programs a number of ways. The easiest is to find an existing image on the Web. Although this is technically very simple, many of the images on the Web are owned by somebody. Respect the intellectual property rights of the original owners. Get permission for any images you use. Another alternative is to create the graphics yourself. Even if you don’t have any artistic talent at all, modern software and technology make it quite easy to generate passable graphics. You can do a lot with a digital camera and a free- ware graphics editor. Even if you hire a professional artist to do graphics for your program, you might still need to be able to sketch what you are looking for. This book’s CD has a couple of very powerful freeware image-editing programs. Creating a Condition On the surface, the behavior of the Ace program is very straightforward: It does something interesting only if the die roll is 1, and it doesn’t do that interesting thing in any other case. While it is a simple idea, the implications are profound. The same simple mechanism in the Ace program is the foundation of all compli- cated computer behavior, from flight simulators to heart monitors. Take a look at the code for the Ace program and see if you can spot the new element: <html> <head> <title>Ace!</title> </head> <body> <h1>Ace!</h1> <h3>Demonstrates if statement</h3> <? $roll = rand(1,6); print “You rolled a $roll”; if ($roll = = 1){ print “<h1>That’s an ace!!!!!</h1>”; } // end if 60 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 FIGURE 3.4 When a 1 appears, the user is treated to a lavish multimedia display. print “<br>”; print “<img src = die$roll.jpg>”; ?> <br> Refresh this page in the browser to roll another die. </body> </html> The secret to this program is the segment that looks like this: if ($roll = = 1){ print “<h1>That’s an ace!!!!!</h1>”; } // end if The line that prints “That’s an ace!!!!!” doesn’t happen every time the program is run. It only happens if a certain condition is true. The if statement sets up a condition for evaluation. In this case, the condition is read $roll is equal to 1. If that condition is true, all the code between the left brace ( {) and the right brace (}) evaluates. If the condition is not true, the code between the braces is skipped altogether. A condition can be thought of as an expression that can be evaluated as true or false. Any expression that can return a true or false value can be used as a con- dition. Most conditions look much like the one in the Ace program. This condi- tion checks the variable $roll to see if it is equal to the value 1. Note that equality is indicated by two equals signs ( ==). This is important, because computer programs are not nearly as flexible as humans. We humans often use the same symbol for different purposes. While computer languages can do this, it often leads to problems. The single equals sign is reserved for assignment. You should read this line as x gets five , indicat- ing that the value 5 is being assigned to the variable $x: $x = 5; This code fragment should be read as x is equal to five, as it is testing equality. $x = = 5; It is essentially asking whether x is equal to 5. A condition such as $x = = 5 does not stand on its own. Instead, it is used inside some sort of other structure, such as an if statement. 61 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s 62 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 Operator Description == equal to < less than > greater than <= less than or equal to >= greater than or equal to != not equal to TABLE 3.1 COMPARISON O PERATORS Exploring Comparison Operators Equality (==) is not the only type of comparison PHP allows. You can compare a variable and a value or two variables using a number of comparison operators. Table 3.1 describes comparison operators. These comparison operators work on any type of data, although the results might be a little strange when you use these mathematical operators on non- numeric data. For example, if you have a condition like the following, you get the result true: “a” < “b” You get that result because alphabetically, the letter a is earlier than b, so it has a “smaller” value. Creating an if Statement An if statement begins with the keyword if followed by a condition inside paren- theses. After the parenthesis is a left brace: {. You can put as many lines of code between the left brace and right brace as you wish. Any code between the braces is executed only if the condition is true. If the condition is false, program control flows to the next line after the right brace. It is not necessary to put a semicolon on a line ending with a brace. It is custom- ary to indent all the code between the left and right braces. . are generated in PHP. Most languages allow you to create a random floating-point value between 0 and 1, and then requires you to transform that value to whatever range you wish. PHP allows—in fact,. simulators to heart monitors. Take a look at the code for the Ace program and see if you can spot the new element: <html> <head> <title>Ace!</title> </head> <body> <h1>Ace!</h1> <h3>Demonstrates. COMPARISON O PERATORS Exploring Comparison Operators Equality (==) is not the only type of comparison PHP allows. You can compare a variable and a value or two variables using a number of comparison