<<

. 24
( 132 .)



>>


?>

This is clear enough. But before we mention the complexities of the if state-
ment, you should know how PHP determines whether a condition is true or false.

Determining true or false in PHP
The next section shows the operators commonly used in if statements. These are
fairly easy to understand. In the preceding code example, 100 is greater than 10, so
($foo > $bar) will test true. No problem. But there™s a bit more to these tests in
PHP.
The words TRUE and FALSE also carry the expected meanings.

if (TRUE)
{
echo “Yup!”; //this will be printed
}
if (FALSE)
{
echo “Nothing doing.”; //this will not be printed
}

But you™re not limited to simple mathematical operators or the words TRUE and
FALSE when you™re testing for a true or false condition. As you can see in Chapter
4, you often test for the existence of a variable using isset() or empty(). These
functions, like many others in PHP, return a value of FALSE if the condition is false,
and a value of TRUE if the condition is true. If used as a simple value, FALSE con-
verts to 0 and TRUE to 1. For example, the following prints out “1”:

$myvar = “I am setting a variable”;
echo isset($myvar), “\n”;

But though FALSE and 0 are equivalent (just as 0 and an empty string are equiv-
alent) and TRUE and 1 are equivalent, they are not the same. You can see this using
Chapter 5: Control Structures 119

the built-in PHP function var_dump(), which shows you the internal representa-
tion of a value. If we use it with the previous example:

$myvar = “I am setting a variable”;
var_dump(isset($myvar));

the output is now “bool(true)”.
When you need to test if two values are not just equivalent, but identical, you
use the === operator (or !== to test if the values are not identical). The following
shows you what we mean:

$myvar = “I™m setting a variable again”;
if (isset($myvar) == 1)
echo “isset(\$myvar) is equivalent to 1\n”;
if (isset($myvar) === 1)
echo “isset(\$myvar) is exactly the same as 1\n”;
if (isset($myvar) == TRUE)
echo “isset(\$myvar) is equivalent to TRUE\n”;
if (isset($myvar) === TRUE)
echo “isset(\$myvar) is exactly the same as TRUE\n”;

The output of this code is:

isset($myvar) is equivalent to 1
isset($myvar) is equivalent to TRUE
isset($myvar) is exactly the same as TRUE

It™s not just 1 that is true ” any non-zero, non-empty value tests as true (an array
with no elements is empty, so it tests as false). This gives you some flexibility in
your tests.
When working with Web pages, you™ll usually be doing some sort of text manip-
ulation. Often you™ll need to test whether the text string you™re working with has a
certain structure. For example, you might want to test whether a string contains
certain characters. You can use one of the regular expression functions for this, but
you can also use the strstr() function. The strstr() function takes two argu-
ments, both of them strings. It searches the first argument for the first occurrence
of the string specified in the second argument. It returns the string in the second
argument plus all of the characters following that string. However, if the string isn™t
found, the function will return a value of FALSE. In the following example
strstr() returns “text string”:

$str = “my little text string”;
strstr($str, “text”);
120 Part II: Working with PHP

Since the result of this function is not empty and not 0 it can be used in a test.
The following code would test TRUE and print out “Yeah!”

$str = “my little text string”;
if (strstr($str, “text”))
{
echo “Yeah!”;
}

But in the string is not found in the following example, so the function will
return a value of FALSE and nothing will print:

$str = “my little text string”;
$new_str = strstr($str, “nothing”);
if ($new_str)
{
echo “nothing to print”; //this will not be printed
}

However, you need to be careful with these kinds of simple tests. For instance,
using strstr() just to test if one string contains another is something of a waste
of resources ” it™s handing you back a whole substring value that you don™t need.
So say you decide to use the strpos() function instead. This built-in function
returns the position of one string within another, or FALSE if the string is not found.
The problem is that the code we™ve used in the previous two examples can produce
some odd results:

$str = “my little text string”;
if (strpos($str, “text”))
{
echo “Found ˜text™\n”;
}
else
{
echo “Did not find ˜text™\n”;
}
if (strpos($str, “my”))
{
echo “Found ˜my™\n”;
}
else
{
echo “Did not find ˜my™\n”;
}
Chapter 5: Control Structures 121

This produces the following output:

Found ˜text™
Did not find ˜my™

But we can see that ˜my™ clearly is inside ˜my little text string™. What
gives?
The problem is that in PHP, string positions start with 0. The string ˜my™ is at the
beginning of ˜my little text string™, and so its position is 0, which is what
strpos() returns. Just testing for zero or non-zero values isn™t good enough. We
need to check explicitly for a return value of FALSE:

if (strpos($str, “my”) !== FALSE)
{
echo “Found ˜my™\n”;
}
else
{
echo “Did not find ˜my™\n”;
}

This produces the correct result:

Found ˜my™

You have to be careful to match your tests to the values you might be testing.
Usually, that™s just a matter of ” surprise! ” checking the documentation.
This is a good place to note that the functions you create in the course of your
programming will often need to return a value indicating success or failure. You
can make your functions do this by returning TRUE or FALSE.
Take a look at this example that looks for http:// at the beginning of a string
(a common task and one that illustrates the technique):

//tests whether a variable starts with “http://”
function url_test ($url)
{
if (strtolower(substr($url,0,7)) == “http://”)
{
return TRUE; }
else
{
return FALSE; }
}
122 Part II: Working with PHP

$myurl = “http://www.theonion.com”;
if (url_test ($myurl))
{
echo “Thanks for entering a valid URL.”;
}


Comparison operators
Table 5-1 lists the relatively few comparison operators in PHP.


TABLE 5-1 PHP™S COMPARISON OPERATORS

Symbol Operator Description

== (2 equals signs) Equal to Determines if two quantities are
equivalent.
=== (3 equals signs) Identical to Determines if two values have
equivalent values and are of the same
variable type.
Not equal Determines if two values are not
!=
equivalent.
Not identical to Determines if two values are not
!==
equivalent, or not of the same variable
type.
Greater than Determines if the value to the left of
>
the symbol is greater than the one to
the right.
Less than Determines if the value to the left of the
<
symbol is less than the one to the right.
Greater than or equal to Determines if the value to the left of
>=
the symbol is greater than or equal to
the one on the right.
Less than or equal to Determines if the value to the left of
<=
the symbol is less than or equal to the
one on the right.
Chapter 5: Control Structures 123


Logical operators
In addition to comparison operators, you will be using logical operators in your
scripts. Table 5-2 lists PHP™s logical operators.


TABLE 5-2 PHP™S LOGICAL OPERATORS

Symbol Example Description

Checks both conditions.
and if ($a ==0 and $b==1)

Same as the previous symbol, but has a
&& if ($a ==0 && $b==1)
higher precedence (see Note below).
Determines if one or the other operand
or if ($a ==0 or $b ==1)
meets the condition.
Same as the previous symbol, but has a
|| if ($a ==0 || $b ==1)
higher precedence (see Note below).
This is known as exclusive or. It determines
xor if ($a ==0 xor $b==1)
if one of the two operands is true but not
both. If both of these conditions are true,
the overall test will be false.
Determines if something is not the case.
! if (!empty($a))
In this example the condition will be true if
$a is not empty.




The difference between && and and, and between || and or, is the order of
precedence. PHP must determine which operators to compare first. It does
this according to the list found at http://php.net/operators. Don™t
forget, though, that parentheses override the order of precedence. The con-
tents of inner parentheses get evaluated before those of outer parentheses.



Complex if statements
Using the operators in Table 5-1 and 5-2, you can create if statements that are a
bit more complex than the basic one at the beginning of this chapter.
124 Part II: Working with PHP

Here are a few quick examples:

if ($var == 1 && $var2 <= 5 && !empty($var3))
{
//do some stuff
}

<<

. 24
( 132 .)



>>