Online Book Reader

Home Category

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

By Root 1567 0
exists. This function returns true if the variable exists and false if it doesn’t.

b. Check each check box variable.

If it exists, the corresponding check box was checked, so she must be a witch (and she must weigh the same as a duck — you’ve really got to watch this movie).

After testing for the existence of all the check boxes, the $witch variable will still be false if none of the check boxes were checked. If any combination of check boxes is checked, $witch will be true:

//determine if she’s a witch

$witch = false;

//See if check boxes exist

if(isset($_REQUEST[“nose”])){

$witch = true;

}

if(isset($_REQUEST[“hat”])){

$witch = true;

}

if(isset($_REQUEST[“newt”])){

$witch = true;

}

if ($witch == true){

print “

She’s a witch!

\n”;

} // end if

Chapter 3: Control Structures

In This Chapter

Getting used to conditions

Using if, else if, and else

Using switch structures

Working with while and for loops

Using comparison operators


Computer programs are most interesting when they appear to make decisions. PHP has many of the same decision-making structures as JavaScript, so if you’ve already looked over Chapters 2 and 3 of Book IV, you will find this chapter very familiar. In any case, take a look at conditions to see the key to making the computer branch and loop.


Introducing Conditions (Again)

Computer programs make decisions. That’s part of what makes them interesting. But all the decisions a computer seems to make were already determined by the programmer. The computer’s decision-making power is all based on an idea called a condition. This little gem is an expression that can be evaluated as true or false. (That sounds profound. I wonder if it will be on the mid-term?)

Conditions can be comparisons of one variable to another, they can be Boolean (true or false) variables, or they can be functions that return a true or false value.

If this talk of conditions is sounding like déjà vu, you’ve probably read about conditions in Book IV, Chapters 2 and 3. You’ll find a lot of the same ideas here; after all, conditions (and branches and loops, and lots of other stuff) are bigger than one programming language. Even though this mini-book covers a different language, you’ll see coverage of the same kinds of things. If you haven’t read that minibook already, you might want to look it over first so you can see how programming remains the same even when the language changes.


Building the Classic if Statement

The if statement is the powerhouse of computer programming. Take a look at Figure 3-1 to see it in action. This program might be familiar if you read Book IV already. It rolls a standard six-sided die, and then displays that die on the screen.

When it rolls a six, it displays an elaborate multimedia event, as shown in Figure 3-2. (Okay, it just says Holy Guacamole! That’s a six!)

This program is much like the duece.html program in Book IV, Chapter 2. I’m talking about exactly the same topic, and do all the same things here as in that program. However, PHP and JavaScript are a little different, and that’s part of the game of programming. Appreciate the concepts that flow between languages while noting those details that are different.

Figure 3-1: This program rolls a die. Try it again.

Figure 3-2:

It’s a six! Joy!

Rolling dice the PHP way

PHP has a random number generator, which works a little differently than the one in JavaScript. The PHP version is actually easier for dice.

$variable = rand(a, b);

This code creates a random integer between a and b (inclusive), so if you want a random 1–6 die, you can use a statement like this:

$die = rand(1,6);

It doesn’t get a lot easier than that.


Checking your six

The code for the if.php program rolls a die, displays an image, and celebrates the joyous occasion of a six.

Return Main Page Previous Page Next Page

®Online Book Reader