Testing Variables

Testing Variables

At the start of this chapter, we showed that assigning data to a variable determines the variable type. The appearance of the variable gives no indication as to what the variable contains. If you see $var sitting in a script you’ll have no idea if this con- tains a string, an integer, a floating-point number, or an array. In fact, many times in your scripts you won’t be sure if the variable contains a value, or even if it exists at all. For all these reasons, you need to perform tests. The following sections describe the types of tests you can perform.

isset( )

This function tests whether a variable has any value, including an empty string. It returns a value of either true or false. If the variable has not been initialized or has not been set, isset() will test false.

Consider the following script, which processes a MySQL query. You already know that database fields can contain both empty strings and null values. It’s quite possible that in your script you would want to treat the two differently. To print out

a special message when the query comes across null values, you would need to use isset(). In this code, $query is a select statement typed into a form element.

$result = mysql_query($query) or die (mysql_error());

$number_cols = mysql_num_fields($result); echo “<b>query: $query</b><br>\n”;

//layout table header echo “<table border = 1>\n”; echo “<tr align=center>\n”; for ($i=0; $i<$number_cols; $i++) {

echo “<th>”, mysql_field_name($result, $i), “</th>\n”; } echo “</tr>\n”;//end table header

//layout table body while ($row = mysql_fetch_row($result)) {

echo “<tr align=left>\n”; for ($i=0; $i<$number_cols; $i++) {

echo “<td>”; if (!isset($row[$i])) //test for null value

{echo “NULL”;}

Chapter 4: Getting Started with PHP —Variables

else {echo $row[$i];} echo “</td>\n”; } echo “</tr>\n”;

} echo “</table>”;

Note that the exclamation point ( ! ) means “not”. So the phrase if(!isset ($var)) will test true if the variable is not set. If you wish to destroy a variable, use the unset function.

empty( )

The empty() function overlaps somewhat with the isset() function. It tests true if

a variable is not set, contains an empty string, or has a value of 0. It is useful for, among other things, processing form data. If you want to determine if the user put something in a text field you could use something like this:

if(empty($first_name)) {

echo “Please enter your first name. It is a required field”; exit;

is_int( )

This tests whether a variable is an integer. It has two synonyms: is_integer() and is_long() . You may need this function to troubleshoot a script when you’re not sure if a variable is an integer or a string containing numerals.

$a = “222”; $b = 22;

Given these two variable assignments, is_int($a) would test false and is_int($b) would test true.

is_double( )

This function tests whether a variable is a floating-point (or double) number. It has two synonyms: is_float() and is_real() .

is_string( )

This function tests whether a variable is a text string.

92 Part II: Working with PHP

is_array( )

This function tests whether a variable is an array. This is used frequently in the course of this book. A good example can be found in Chapter 6, in the discussion of the implode() function.

is_bool( )

This tests whether a variable is boolean, (contains either TRUE or FALSE). Note that the following examples are not boolean.

$a = “TRUE”; $b = “FALSE”;

In Chapter 6 you will see a variety of functions that return FALSE on failure. In these, FALSE is a boolean value.

is_object( )

Returns true if the variable is an object. See Chapter 6 for a discussion of objects and object-oriented programming if you don’t know what an object is.

gettype( )

This function will tell you the type of variable you have. It will return the expected values (string, double, integer, array, or boolean), and it can also return types related to object-oriented programming (object, class). There will be more informa- tion on PHP object-oriented programming in Chapter 6.

Note that gettype() returns a string. So the following would test TRUE and print “Yes”.

$str = “I am a string”; $type = gettype($str); if ($type == “string”) {

echo “Yes”; }