Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [213]

By Root 1544 0
you remember how the structure is organized.

4. Establish the first case.

Put the first value you want to check for. In this situation, I’m looking for the value 1. Note that the type of data matters, so be sure you’re comparing against the same type of data you think the variable will contain. Use a colon (:) to indicate the end of the case. This is one of the rare situations where you do not use a semicolon or brace at the end of a line.

5. Write code that should happen if the expression matches the case.

If the expression matches the case (for example, if $yourNumber is equal to 1), the code you write here will execute.

6. End the code with the break statement.

When you use an if-else if structure to work with multiple conditions, the interpreter jumps out of the system as soon as it encounters the first true condition. Switches work differently. Unless you specify (with the break statement), code will continue to evaluate even when one of the expressions is matched. You almost always need the break statement.

7. Use the default clause to handle any unexpected behavior.

The default section of the switch structure is used to handle any situation that wasn’t covered by one of the previously defined cases. It’s a good idea to always include a default clause.

It may seem odd to have a default clause in this example. After all, I know how the rand() function works, and I know that I’ll get values only between 1 and 8. It shouldn’t be possible to have a value that isn’t covered by one of the cases, yet I have a default clause in place for exactly that eventuality. Even though something shouldn’t ever happen, sometimes it does. At the very least, I want a nice piece of code to explain what happened and send some kind of error message. If it’s an important problem, I may have the code quietly e-mail me a message letting me know what went wrong.

You might wonder whether the switch is necessary at all. I could have used the interpolation tricks shown in the dice example to get the necessary images. However, remember that XHTML requires all images to have alt tags. With dice, the value of the roll is a perfectly acceptable alt value. The Magic 8 Ball needs to return text if the image doesn’t work properly. I used a switch to ensure that I have the appropriate alt text available. (Extra points if you think an array would be an even better way to handle this situation.)


Looping with for

Sometimes you want to repeat something. PHP (like most languages) supports a number of looping constructs. Begin with the humble but lovable for loop, as shown in Figure 3-8.

Figure 3-8: This page prints a lot of dice with a for loop.

As you can see, Figure 3-8 prints a lot of dice. In fact, it prints 100 dice. This would be tedious to do by hand, but that’s exactly the kind of stuff computers are so good at.

The following code explains all:

PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

for.php

Dice Rolling Game

Welcome to the dice rolling game. Rolling 100 dice. How many will be sixes?

$sixCount = 0;

for ($i = 0; $i < 100; $i++){

$userNumber = rand(1,6);

print <<< HERE

alt = “$userNumber“ />

HERE;

if($userNumber == 6){

$sixCount++;

} // end if

} // end for

print “

You rolled $sixCount six(es)!

“;

?>

Try Again!

Most of the code is plain-old HTML. Note the lone print statement responsible for printing out dice. That print statement (and a few supporting characters) are repeated 100 times. for loops are extremely powerful ways to get a lot of work done.

1. Begin with the for keyword.

This keyword indicates the beginning of the for structure.

for ($i = 0; $i < 100; $i++){

Return Main Page Previous Page Next Page

®Online Book Reader