Testing Data Types
Testing Data Types
Different types of data take up different amounts of memory and may be treated differently when they are
manipulated in a script. Some programming languages therefore demand that the programmer declare in
advance which type of data a variable will contain. By contrast, PHP is loosely typed, meaning that it will
determine the data type at the time data is assigned to each variable.
This automatic typing is a mixed blessing. On the one hand, it means that variables can be used flexibly—in
one instance, a variable can hold a string and then later in the script it can hold an integer or some other data
type. On the other hand, this flexibility can lead to problems in larger scripts if you are specifically expecting a
variable to hold one data type when in fact it holds something completely different. For example, suppose that
you have created code to manipulate an array variable. If the variable in question instead contains a number
value and no array structure is in place, errors will occur when the code attempts to perform array-specific
operations on the variable.
Table 5.1 shows the eight standard data types available in PHP.
PHP has several functions available to test the validity of a particular type of variable—one for each type, in
fact. The is_* family of functions, such as is_bool(), tests whether a given value is a Boolean. Listing 5.1
assigns different data types to a single variable and then tests the variable with the appropriate is_* function.
The comments in the code show you where the script is in the process.
1: <?php
4: echo "<br/>";
5: $testing = 5;
7: echo "<br/>";
8: $testing = "five";
26: ?>
Put these lines into a text file called testtype.php, and place this file in your web server document root.
When you access this script through your web browser, it produces the following output:
is null? 1
is an integer? 1
is a string? 1
is a double? 1
is boolean? 1
is an array? 1
is numeric?
is a resource?
is an array? 1
When the $testing variable is declared in line 2, no value is assigned to it, so when the variable is tested in
line 3 to see whether it is null (using is_null()), the result is 1 (true). After this, values are assigned
to $testing by using the = sign before testing it with the appropriate is_* function. An integer, assigned to
the $testing variable in line 5, is a whole or real number. In simple terms, you can think of a whole number
as a number without a decimal point. A string, assigned to the $testing variable in line 8, is a collection of
characters. When you work with strings in your scripts, they should always be surrounded by double or single
quotation marks (" or '). A double, assigned to the $testing variable in line 11, is a floating-point number
(that is, a number that includes a decimal point). A Boolean, assigned to the $testing variable in line 14, can
have one of two special values: true or false. In line 17, an array is created using the array() function,
which you’ll learn more about in Chapter 8, “Working with Arrays.” This particular array contains three items,
and the script dutifully reports $testing to have a type of “array.”
From line 20 through the end of the script, no value is reassigned to $testing—only the type is tested. Lines
20 and 22 test whether $testing is a numeric or resource type, respectively, and because it is not, no value
is displayed to the user. In line 24, the script tests again to see whether $testing is an array, and because it
is, the value of 1 is displayed.
Changing Type with settype()
PHP also provides the function settype(), which is used to change the type of a variable. To
use settype(), you place the variable to change and the type to change it to between the parentheses and
Listing 5.2 converts the value 3.14 (a float) to each of the four standard types..
Listing 5.2. Changing the Type of a Variable with settype()
1: <?php
2: $undecided = 3.14;
4: settype($undecided, 'string');
6: settype($undecided, 'integer');
8: settype($undecided, 'double');
In each case, we use the appropriate is_* function to confirm the new data type and to print the value of the
variable $undecided to the browser using echo. When we convert the string "3.14" to an integer in line 6,
any information beyond the decimal point is lost forever. That’s why $undecided contains 3 after we change
it back to a double in line 8. Finally, in line 10, we convert $undecided to a Boolean. Any number other
than 0 becomes true when converted to a Boolean. When printing a Boolean in PHP, true is represented
as 1 and false is represented as an empty string, so in line 11, $undecided is printed as 1.
Put these lines into a text file called settype.php and place this file in your web server document root. When
you access this script through your web browser, it produces the following output:
is 3.14 a double? 1
is 3.14 a string? 1
is 3 an integer? 1
is 3 a double? 1
is 1 a boolean? 1
type by casting is the fact that casting produces a copy, leaving the original variable untouched. To change
type through casting, you indicate the name of a data type, in parentheses, in front of the variable you are
copying. For example, the following line creates a copy of the $originalvar variable, with a specific type
(integer) and a new name $newvar. The $originalvar variable will still be available, and will be its original
type; $newvar is a completely new variable.
1: <?php
2: $undecided = 3.14;
16: ?>
Listing 5.3 never actually changes the type of the $undecided variable, which remains a double throughout
this script, as illustrated on line 15, where the gettype() function is used to determine the type
of $undecided.
In fact, casting $undecided creates a copy that is then converted to the type specified at the time of the cast,
and stored in the variable $holder. This casting occurs first in line 3, and again in lines 5, 7, 9, and 11.
Because the code is working with only a copy of $undecided and not the original variable, it never lost its
original value, as the $undecided variable did in line 6 of Listing 5.2 when its type changed from a string to
an integer.
Put the contents of Listing 5.3 into a text file called casttype.php and place this file in your web server
document root. When you access this script through your web browser, it produces the following output:
is 3.14 a double? 1
is 3.14 a string? 1
is 3 an integer? 1
is 3.14 a double? 1
is 1 a boolean? 1
Now that you’ve seen how to change the contents of a variable from one type to another either by
using settype() or by casting, consider why this might be useful. It is not a procedure that you will have to
use often because PHP automatically casts your variables for you when the context of the script requires a
change. However, such an automatic cast is temporary, and you might want to make a variable persistently
hold a particular data type—thus, the ability to specifically change types.
For example, the numbers that a user types into an HTML form will be made available to your script as the
string type. If you try to add two strings together because they contain numbers, PHP will helpfully convert
these strings into numbers while the addition is taking place. So "30cm" + "40cm"
During the casting of a string into an integer or float, PHP will ignore any non-numeric characters. The string
will be truncated, and any characters from the location of the first non-numeric character onward are ignored.
So, whereas "30cm" is transformed into "30", the string "6ft2in" becomes just 6 because the rest of the
$test = "30cm";
As you can see, the user has added units to his number—instead of entering "30", the user has
entered "30cm". You can make sure that the user input is clean by casting it as an integer:
Had the user input not been cast, and the value of the original variable, $test, been used in place
of $newtest when printing the statement regarding the width of a box, the result would have been
This output looks strange; in fact, it looks like parroted user input that hadn’t been cleaned up (which is exactly
what it is).
Example”
The settype() function is a built-in function in PHP. The settype() function is
used to the set the type of a variable. It is used to set type or modify type of an
existing variable.
Syntax:
boolean settype($variable_name, $type)
Program 1:
<?php
$var1 = "123xyz";
$var2 = 3;
$r = true;
settype($var1, "integer");
settype($var2, "float");
settype($r, "string");
echo $var1."\n";
echo $var2."\n";
echo $r."\n";
?>
Output:
123
3
1
Program 2:
<?php
$var1 = "a12b";
$var2 = 3.566;
$r = true;
settype($var1, "integer");
settype($var2, "integer");
settype($r, "string");
echo $var1."\n";
echo $var2."\n";
echo $r."\n";
?>
Output:
0
3
1