Flylib.com

Books Software

 
 
 

Using if Statements


Using if Statements

Here's where you can start making choices based on your data: by using if statements. Just about all high-level programming languages, including PHP, have an if statement, and here's what it looks like formally :

if (

expression

)
    statement

Here, expression is an expression that can be evaluated to a boolean trUE / FALSE value. For example, if expression were 5 > 2 , then it would evaluate to TRUE because 5 is greater than 2 . If expression is TRUE , then statement is executed; if expression is FALSE , statement is not executed.

The if statement is great because your script can make choices at run time, based on the run-time value of your data, which can include text the user has entered in a web page (such as a password), values retrieved from a database (such as inventory available), or even data fetched from another web site (such as stock prices).

Here's an example that tests whether the value in a variable named $temperature is less than 80 degrees:

<?php
    $temperature = 75;

if ($temperature < 80)


echo "Nice day.";

?>

In this case, $temperature holds a value of 75 , so the statement echo "Nice day."; is executed, and this is what you see:

Nice day.

But what if you have multiple statements you want to execute if a certain condition is true? That's no problema PHP statement can also be a compound statement , which means it can contain any number of simple statements, as long as you enclose them in curly braces.

For example, here are three simple statements made into one compound PHP statement just by enclosing them between { and } :

{
    echo "Your time is up!<BR>";
    echo "Please put the phone down.";
    $hang_up_now = TRUE;
}

You can see how this new compound statement might appear inside an if statement in phpif.php, Example 2-5.

Example 2-5. Using the if statement, phpif.php
<HTML>
    <HEAD>
        <TITLE>Using the if statement</TITLE>
    </HEAD>

    <BODY>
        <H1>Using the if statement</H1>
        <?php

$minutes= 4;


if($minutes > 3) {


echo "Your time is up!<BR>";


echo "Please put the phone down.";


$hang_up_now = TRUE;


}

?>
    </BODY>
</HTML>

And you can see this new example at work in Figure 2-6.

Figure 2-6. Using the if statement.


Here's another example. PHP has functions named is_int , is_float , is_array , and so on to return the type of a variable. Using them, you can test the type of a variable before using it, like this:

if (is_int($variable)) 
    $variable = $variable + 10;

The if statement is a basic one that you'll use in practically all your scriptsand more on it is coming up in this chapter.


The Comparison Operators

There is an entire set of operators designed to be used with the if statementthe comparison operators . In the previous chunk 's example, we used the greater-than ( > ) operator to check the value in $minutes :

<?php
    $minutes= 4;

if($minutes > 3) {


echo "Your time is up!<BR>";


echo "Please put the phone down.";


$hang_up_now = TRUE;


}

?>

Comparison operators let you compare two values like this. All the PHP comparison operators are shown in Table 2-3.

Table 2-3. The Comparison Operators

Operator

Operation

Example

Result

==

Equal

$a == $b

trUE if $a is equal to $b .

===

Identical

$a === $b

trUE if $a is equal to $b , and they are of the same type.

!=

Not equal

$a != $b

TRUE if $a is not equal to $b .

<>

Not equal

$a <> $b

TRUE if $a is not equal to $b .

!==

Not identical

$a !== $b

trUE if $a is not equal to $b , or they are not of the same type.

<

Less than

$a < $b

trUE if $a is strictly less than $b .

>

Greater than

$a > $b

TRUE if $a is strictly greater than $b .

<=

Less than or equal to

$a <= $b

trUE if $a is less than or equal to $b .

>=

Greater than or equal to

$a >= $b

trUE if $a is greater than or equal to $b .


For example, if you wanted to check whether the value in the variable $temperature was exactly 75 degrees, you could use == , the equality operator:

<?php
    $temperature = 75;

if ($temperature == 75) {


echo "75 degrees today, not too bad.";


}

?>

NOTE

Note that the equality operator is ==, not the assignment operator, = . If you're not getting the results you want when you think you're using the equality operator in an if statement, check to make sure you haven't actually used the assignment operator instead.


Here's another example, this time using the not equal operator, != , which returns trUE if two values are not equal. In this case, we're checking to make sure that the temperature is not 75 degrees:

<?php
    $temperature = 80;

if ($temperature != 75) {


echo "It's not 75 degrees today.";


}

?>

The results:

It's not 75 degrees today.

Here's an important detail about floating point precision. Internally, floating point numbers are stored in binary, not decimal, format. That means the floating point number that you think is 8.00000000 is really stored internally as 7.99999999, which is very close, but separate enough to make an equality ( == ) test fail. Usually, decimal values such as 1.75 or 3.12 cannot be converted into exactly the same number in the internal binary format your computer uses, which means that you can't expect to rely on floating point values down to the last digit, or even the last couple of digits, for complete precision.

So how can you compare floating point numbers for equality? The easiest way is to compare them and make sure they are within a certain range. For example, say you have a value that you're using for pi: $value = 3.1415926535; . PHP comes with a built-in function named pi (see "The Math Functions" in this chapter) that will return the value of pi to fairly high precision ( 3.1415926535898 ), so we can check our value against that by checking their difference using the abs function to get the absolute value (also see "The Math Functions" in this chapter for the abs function):

<?php

$value = 3.1415926535;


if (abs($value - pi()) < .0000001) {


echo "Close enough";

}
?>