Online Book Reader

Home Category

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

By Root 1488 0

if.php

Try to roll a six

roll again

$roll = rand(1,6);

print <<

alt = ”$roll” />

HERE;

if ($roll == 6){

print(”

Holy Guacamole! That’s a six!

\n”);

} // end if

?>

The process is eerily familiar:

1. Begin with a standard XHTML template.

As always, PHP is encased in XHTML. There’s no need to switch to PHP until you get to the part that HTML can’t do: that is, rolling dice and responding to the roll.

2. Add a link to let the user roll again.

Add a link that returns to the same page. When the user clicks the link, the server refreshes the page and rolls a new number.

3. Roll the rand() function to roll a die. Put the result in a variable called $roll.

4. Print out a graphic by creating the appropriate tag.

I preloaded a bunch of die images into a directory called images. Each image is carefully named die1.jpg through die6.jpg. To display an image in PHP, just print out a standard img tag. The URL is created by interpolating the variable $roll into the image name. Don’t forget that XHTML requires an alt attribute for the img tag. I just use the $roll value as the alt. That way, the die roll will be known even if the image doesn’t work.

5. Check whether the die is a six.

This is where the condition comes in. Use the if statement to see whether the value of $roll is 6. If so, print out a message.

The == (two equal sign) means “is equal to.” A single equal sign means assignment. If you use the single equal sign in a condition, the code may not crash, but it probably won’t do what you intended.

The else clause is used when you want to do one thing if a condition is true and something else if the condition is false. The highLow.php program shown in Figure 3-3 handles this kind of situation.

Figure 3-3: This program tells whether the roll was high or low.

The code is very similar to the if.php program.

The bold code shows the only part of the program that’s new.

highLow.php

High or low?

roll again

$roll = rand(1,6);

print <<

alt = “$roll“ />

HERE;

if ($roll > 3){

print “

You rolled a high one

\n”;

} else {

print “

That’s pretty low

\n”;

} // end if

?>

Most of the code for this program is the same as the previous code example, but the condition is slightly different:

♦ Now the condition is an inequality. I now use the greater-than symbol (>) to compare the roll to the value 3. You can use any of the comparison operators in Table 3-1. If $roll is higher than 3, the condition will evaluate as true, and the first batch of code will run.

♦ Add an else clause.

The else clause is special because it handles the situation when the condition is false. All it does is set up another block of code.

♦ Include code for the false condition.

The code between else and the ending brace for if ending brace will run only if the condition is evaluated false.

Understanding comparison operators

PHP uses the same comparison operators as JavaScript (and many other languages based on C). Table 3-1 summarizes these operators.

Table 3-1 Comparison Operators

Comparison

Discussion

A == B

True if A is equal to B

A != B

True if A is not equal to B

A < B

True if A is less than B (if they are numeric) or earlier in the alphabet (for strings)

A > B

True if A is larger than B (numeric) or later in the alphabet (string)

A

Return Main Page Previous Page Next Page

®Online Book Reader