Boolean is a type, having two values, denoted as
This code sets the value of
false are not case sensitive, so
FALSE can be used as well, even
FaLsE is possible. Using lower case is most common and recommended in most code style guides, e.g. PSR-2.
Booleans can be used in if statements like this:
Due to the fact that PHP is weakly typed, if
$foo above is other than
false, it's automatically coerced to a boolean value.
The following values result in
- a zero value:
- an empty string
null(the content of an unset variable, or assigned to a variable)
Any other value results in
To avoid this loose comparison, you can enforce strong comparison using
===, which compares value and type. See Type Comparison for details.
To convert a type into boolean, you can use the
(boolean) cast before the type.
or call the
Boolean conversion to a string (note that
false yields an empty string):
Boolean conversion to an integer:
Note that the opposite is also possible:
Also all non-zero will return true:
Callables are anything which can be called as a callback. Things that can be termed a "callback" are as follows:
Standard PHP functions (note: not language constructs)
non-static Classes (using an alternate syntax)
Specific Object/Class Methods
Objects themselves, as long as the object is found in key
0of an array
Example Of referencing an object as an array element:
Callbacks can be denoted by
callable type hint as of PHP 5.4.
For historical reasons "double" is returned by
gettype()in case of a float, and not simply "float"
Floats are floating point numbers, which allow more output precision than plain integers.
Floats and integers can be used together due to PHP's loose casting of variable types:
php does not show float as float number like other languages, for example:
Floating point precision
(From the PHP manual page)
Floating point numbers have limited precision. Although it depends on the system, PHP typically give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and error propagation must be considered when several operations are compounded.
Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2 (binary), which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small 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, since the internal representation will be something like 7.9999999999999991118....
So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.
Integers in PHP can be natively specified in base 2 (binary), base 8 (octal), base 10 (decimal), or base 16 (hexadecimal.)
Integers are 32 or 64 bits long, depending on the platform. The constant
PHP_INT_SIZE holds integer size in bytes.
PHP_INT_MAX and (since PHP 7.0)
PHP_INT_MIN are also available.
Integer values are automatically created as needed from floats, booleans, and strings. If an explicit typecast is needed, it can be done with the
Integer overflow will be handled by conversion to a float:
There is no integer division operator in PHP, but it can be simulated using an implicit cast, which always 'rounds' by just discarding the float-part. As of PHP version 7, an integer division function was added.
(Note that the extra parentheses around
(25 / 4) are needed because the
(int) cast has higher precedence than the division)
PHP represents "no value" with the
null keyword. It's somewhat similar to the null pointer in C-language and to the NULL value in SQL.
Setting the variable to null:
Checking if the variable was set to null:
Null vs undefined variable
If the variable was not defined or was unset then any tests against the null will be successful but they will also generate a
Notice: Undefined variable: nullvar:
Therefore undefined values must be checked with
A resource is a special type of variable that references an external resource, such as a file, socket, stream, document, or connection.
There are different (sub-)types of resource. You can check the resource type using
You can find a complete list of built-in resource types here.
A string in PHP is a series of single-byte characters (i.e. there is no native Unicode support) that can be specified in four ways:
Displays things almost completely "as is". Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, one can escape it with a back slash ', and to display a back slash, one can escape it with another backslash \
Unlike a single-quoted string, simple variable names and escape sequences in the strings will be evaluated. Curly braces (as in the last example) can be used to isolate complex variable names.
In a heredoc string, variable names and escape sequences are parsed in a similar manner to double-quoted strings, though braces are not available for complex variable names. The start of the string is delimited by
identifier, and the end by
identifier is any valid PHP name. The ending identifier must appear on a line by itself. No whitespace is allowed before or after the identifier, although like any line in PHP, it must also be terminated by a semicolon.
A nowdoc string is like the single-quoted version of heredoc, although not even the most basic escape sequences are evaluated. The identifier at the beginning of the string is wrapped in single quotes.
PHP will generally correctly guess the data type you intend to use from the context it's used in, however sometimes it is useful to manually force a type. This can be accomplished by prefixing the declaration with the name of the required type in parenthesis:
But be carefull: not all type casts work as one might expect:
There are two types of comparison: loose comparison with
== and strict comparison with
===. Strict comparison ensures both the type and value of both sides of the operator are the same.
You can also use strong comparison to check if type and value don't match using
A typical example where the
== operator is not enough, are functions that can return different types, like
strpos, which returns
false if the
searchword is not found, and the match position (
PHP is a weakly-typed language. It does not require explicit declaration of data types. The context in which the variable is used determines its data type; conversion is done automatically: