Introduction
PHP supports eight primitive types.
- Four scalar types:
- Boolean
- integer
- float (floating-point number, aka 'double')
- string
- Two compound types:
- And finally two special types:
- This manual also introduces some pseudo-types for readability reasons:
Consider double
the same as float, the two names exist only for historic reasons.
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.
Note: If you want to check out the type and value of a certain expression,
use var_dump().
Note: 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:
|
Floating point numbers (AKA "floats",
"doubles" or "real numbers") can be specified using any of the
following syntaxes:
Formally:
The size of a float is platform-dependent, although a maximum of
~1.8e308 with a precision of roughly 14 decimal digits is a common
value (that's
64
bit IEEE format).
| Floating point
precision |
|
It is quite usual that simple decimal
fractions like 0.1
or 0.7 cannot
be converted into their internal binary counterparts without a
little loss of precision. This can lead to confusing results:
for example,
floor((0.1+0.7)*10) will usually return
7 instead of
the expected 8
as the result of the internal representation really being
something like
7.9999999999....
This is related to the fact that it is
impossible to exactly express some fractions in decimal
notation with a finite number of digits. For instance,
1/3 in decimal
form becomes
0.3333333. . ..
So never trust floating number results
to the last digit and never compare floating point numbers for
equality. If you really need higher precision, you should use
the
arbitrary precision
math functions or
gmp functions instead. |
For information on when and how
strings are converted to floats, see the section titled
String conversion to numbers. For values of other types, the
conversion is the same as if the value would have been converted
to integer and then to float. See the
Converting to integer section for more information. As of PHP
5, notice is thrown if you try to convert object to float.
|
|
An
integer is a number of
the set Z = {..., -2, -1, 0, 1, 2, ...}.
See also:
Arbitrary length integer / GMP,
Floating point numbers, and
Arbitrary precision / BCMath
Integers can be specified in decimal
(10-based), hexadecimal (16-based) or octal (8-based) notation,
optionally preceded by a sign (- or +).
If you use the octal notation, you must
precede the number with a
0 (zero), to use hexadecimal notation precede the number
with 0x.
Example 11-1. Integer literals
<?php
$a =
1234;
//
decimal number
$a
= -123;
// a
negative number
$a
=
0123;
//
octal number (equivalent to 83 decimal)
$a
=
0x1A;
//
hexadecimal number (equivalent to 26 decimal)
?>
|
|
Formally the possible structure for integer literals is:
The size of an integer is platform-dependent, although a maximum
value of about two billion is the usual value (that's 32 bits
signed). PHP does not support unsigned integers.
| Warning |
|
If an invalid digit is passed to
octal integer (i.e. 8 or 9), the rest of the number is
ignored.
Example 11-2. Octal weirdness
<?php
var_dump(01090);
// 010 octal = 8 decimal
?> |
|
|
If you specify a number beyond the bounds
of the
integer type, it will
be interpreted as a
float instead. Also,
if you perform an operation that results in a number beyond the
bounds of the
integer type, a
float will be returned
instead.
| Warning |
|
Unfortunately, there was a bug in PHP
so that this does not always work correctly when there are
negative numbers involved. For example: when you do
-50000 * $million,
the result will be
-429496728. However, when both operands are positive
there is no problem.
This is solved in PHP 4.1.0. |
There is no integer division operator in
PHP. 1/2 yields the
float
0.5. You can cast
the value to an integer to always
round it downwards, or you can use the
round() function.
To explicitly convert a value to
integer, use either
the (int) or the
(integer) cast.
However, in most cases you do not need to use the cast, since a
value will be automatically converted if an operator, function or
control structure requires an
integer argument. You
can also convert a value to integer with the function
intval().
See also
type-juggling.
FALSE will yield
0 (zero), and
TRUE will
yield 1 (one).
When converting from float to integer,
the number will be rounded
towards zero.
If the float is beyond the boundaries of
integer (usually +/-
2.15e+9 = 2^31), the result is undefined, since the float
hasn't got enough precision to give an exact integer result. No
warning, not even a notice will be issued in this case!
| Caution |
|
Behaviour of converting to integer
is undefined for other types. Currently, the behaviour is
the same as if the value was first
converted to boolean. However, do
not
rely on this behaviour, as it can change without notice.
|
|
|