Types
- ????
- Introduction
- Booleans
- Integers
- Floating point numbers
- Strings
- Arrays
- Objects
- Resource
- NULL
- Pseudo-types used in this documentation
- Type Juggling
Introduction
PHP supports eight primitive types.
Four scalar types:
-
boolean
-
integer
-
float (floating-point number, aka 'double')
-
string
-
array
-
object
-
resource
-
NULL
-
mixed
-
number
-
callback
The type of a variable is usually not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used.
????: If you want to check out the type and value of a certain expression, use var_dump().
????: If you simply want a human-readable representation of the type for debugging, use gettype(). To check for a certain type, do not use gettype(), but use the is_type functions. Some examples:
$bool = TRUE; // a boolean
$str = "foo"; // a string
$int = 12; // an integer
echo gettype($bool); // prints out "boolean"
echo gettype($str); // prints out "string"
// If this is an integer, increment it by four
if (is_int($int)) {
$int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($bool)) {
echo "String: $bool";
}
?>
If you would like to force a variable to be converted to a certain type, you may either cast 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. Also, you may be interested in viewing the type comparison tables, as they show examples of various type related comparisons.



Aucun commentaire:
Enregistrer un commentaire