CHAPTER 3 ■ OBJECT BASICSTable 3-1. Primitive Types and Checking Functions in PHP
Type Checking Function Type Descriptionis_bool() Boolean One of the two special values true or falseis_integer() Integer A whole numberis_double() Double A floating point number (a number with a decimal point)
is_string() String Character datais_object() Object An object
is_array() Array An arrayis_resource() Resource
A handle for identifying and working with external
resources such as databases or files
is_null() Null An unassigned valueChecking the type of a variable can be particularly important when you work with method and
function arguments.
Primitive Types Matter: An Example
You need to keep a close eye on type in your code. Here’s an example of one of the many type-related
problems that you could encounter.
Imagine that you are extracting configuration settings from an XML file. The
element tells your application whether it should attempt to resolve IP addresses to domain names, a
useful but relatively expensive process in terms of time. Here is some sample XML:
The string "false" is extracted by your application and passed as a flag to a method called
outputAddresses(), which displays IP address data. Here is outputAddresses():
class AddressManager {
private $addresses = array( "209.131.36.159", "74.125.19.106" );
function outputAddresses( $resolve ) {
foreach ( $this->addresses as $address ) {
print $address;
if ( $resolve ) {
print " (".gethostbyaddr( $address ).")";
}
print "\n";
}
}
}