Prev | Current Page 145 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Its prototype follows:
string gettype (mixed var)
Converting Types
The settype() function converts a variable, specified by var, to the type specified by
type. Seven possible type values are available: array, boolean, float, integer, null,
object, and string. If the conversion is successful, TRUE is returned; otherwise, FALSE is
returned. Its prototype follows:
boolean settype(mixed var, string type)
72 CHAPTER 3 ?–  PHP B ASICS
Type Identifier Functions
A number of functions are available for determining a variable??™s type, including
is_array(), is_bool(), is_float(), is_integer(), is_null(), is_numeric(), is_object(),
is_resource(), is_scalar(), and is_string(). Because all of these functions follow the
same naming convention, arguments, and return values, their introduction is consolidated
into a single example. The generalized prototype follows:
boolean is_name(mixed var)
All of these functions are grouped in this section because each ultimately accomplishes
the same task. Each determines whether a variable, specified by var, satisfies
a particular condition specified by the function name. If var is indeed of the type tested
by the function name, TRUE is returned; otherwise, FALSE is returned. An example
follows:
$item = 43;
printf("The variable \$item is of type array: %d
", is_array($item));
printf("The variable \$item is of type integer: %d
",
is_integer($item));
printf("The variable \$item is numeric: %d
", is_numeric($item));
?>
This code returns the following:
The variable $item is of type array: 0
The variable $item is of type integer: 1
The variable $item is numeric: 1
You might be wondering about the backslash preceding $item.


Pages:
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157