Introduction (original) (raw)
PHP supports ten primitive types.
Four scalar types:
Four compound types:
And finally two special types:
This manual also introduces somepseudo-types for readability reasons:
- mixed
- number
- callback (aka callable)
- array|object
- void
And the pseudo-variable $...
.
Some references to the type "double" may remain in the manual. Consider double the same as float; the two names exist only for historic reasons.
The type of a variable is not usually set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used.
Note: To check the type and value of anexpression, use thevar_dump() function.
To get a human-readable representation of a type for debugging, use thegettype() function. To check for a certain type, do_not_ use gettype(), but rather the
is_type
functions. Some examples:`<?php
$a_bool = TRUE; // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
anint=12;//anintegerechogettype(an_int = 12; // an integerecho gettype(anint=12;//anintegerechogettype(a_bool); // prints out: boolean
echo gettype($a_str); // prints out: string// If this is an integer, increment it by four
if (is_int($an_int)) {
$an_int += 4;
}// If $a_bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
echo "String: $a_bool";
}
?> `
To forcibly convert a variable to a certain type, eithercast the variable or use the settype() function on it.
Note that a variable may be evaluated with different values in certain situations, depending on what type it is at the time. For more information, see the section on Type Juggling. The type comparison tables may also be useful, as they show examples of various type-related comparisons.