
In PHP, the
FALSE
and
NULL
values and their associated values are different from how they are commonly used in other languages and have their own non-obvious features.
The article discusses these features.
For beginners, this may be useful for the appearance of a complete picture, for experienced readers - to update in memory if some nuance has flown out of my head.
FALSE
in If
statements
According to the
PHP documentation, the following values are FALSE after casting to type
boolean
:
- the boolean value itself is FALSE
- integer
0
- float
0.0
- empty string ( string ) (
""
) and string "0"
. - empty array ( array ) - array ().
- an object with zero member variables (only in PHP 4, not covered in the article)
- special NULL value (including unspecified variables)
- SimpleXML objects (not covered in this article)
This means that if such values are passed to the condition:
if (…) echo “1”; else echo “0”;
then the string
"0"
will be displayed.
If the value of the variable is not set (
unset
), then a warning may also be issued. Recall that a warning in a condition can be removed by writing in front of the
@
variable.
')
For example:
if (@$undefVar) { …}
But it is worth using
@
only in extreme cases when you have thought well and there are no other suitable options. See the
isset()
function.
The is_null()
function and the isset()
and empty()
language constructs
is_null () returns
TRUE
only for variables to which no value is assigned or to which
NULL
assigned.
isset () returns one-to-one opposite boolean values compared to
is_null()
.
If a variable is not assigned a value, then
is_null()
also issues a warning
«Undefined variable»
, unlike
isset()
, which does not issue any warning.
Recall that in order to remove the value of a variable, you can use the
unset () function. You can also assign a
NULL
value to this end to avoid compiler warnings when trying to read a variable value.
Please note that in contrast to variables for working with constants it is necessary to use the
defined () construction.
With
empty (), everything is simple - returns
TRUE
for true values,
FALSE
for all others, completely in accordance with the list above,
taken from the documentation . By the way, for those who will experiment on their own, only the variable can be passed to
empty()
argument. At attempt to transfer a constant the compiler produces a syntax error.
Array indexing
When working with arrays, all false values will refer to a single element.
The only exceptions are the values of the empty string indexes (
''
) and
NULL
- for these two values a separate memory location is used.
Also, the value of an empty array of
array()
, as well as any other array, cannot be an array index — the compiler issues a warning
«Illegal offset type»
and the value corresponding to such an index is not set.
In order for the indices of the empty string and
NULL
correspond to different elements in the array, you can use the following approach. We describe the function:
function index($var) { if ($var === NULL) return 'NULL'; return 'V' . $var; }
Next, when setting values, we will get separate indexes for the empty string and
NULL
values, more precisely, we will emulate different indexes:
$arr[index('')]='value for empty string '; $arr[index(NULL)]='value for NULL';
Array indices are a separate topic. I will give only an example of the fact that the value with the integer index
$arr[555]
coincides with the value corresponding to the string index
$arr['555']
in the array.
Such features are primarily related to the fact that the indices of the arrays are either integer or string. More detailed information can be found in the
description of the type of array in the php manual .
String representation
Consider the string representation of false constants.
For example, during concatenation, the values are converted to the following lines, as shown in the table below:
Value | Line |
---|
FALSE | (empty line) |
0 | 0 |
0.0 | 0 |
"" | (empty line) |
"0" | 0 |
array () | Array |
Null | (empty line) |
@ $ undef | (empty line) |
The topic of converting to strings is described in more detail on the official website in the paragraph
Converting to string .
Comparison operators
Let us turn to
the comparison operators .
All false values, as expected, return true when compared with the
FALSE
value using the "
==
" operator.
But one should not count on transitivity here when comparing false string constants among themselves.
Let us give the complete table of comparison of false values (plus are the elements of the table, which, when compared with the "
!=
" Operator, return the true value:
! = | FALSE | 0 | 0.0 | "" | "0" | array () | Null | @ $ undef |
---|
FALSE | | | | | | | | |
---|
0 | | | | | | + | | |
---|
0.0 | | | | | | + | | |
---|
"" | | | | | + | + | | |
---|
"0" | | | | + | | + | + | + |
---|
array () | | + | + | + | + | | | |
---|
Null | | | | | + | | | |
---|
@ $ undef | | | | | + | | | |
---|
$undef
variable that was not assigned a value
From the table, you can make some pleasant conclusions:
1. If we know that we use only strings, then we can safely compare them and not worry that
""
(empty string) will be equal to
"0"
.
2. Arrays are never equal to strings, integers, and real numbers.
Since the type of different false constants is different, then in order to distinguish them you can use a couple of operators
===
and
!==
.
The
===
operator returns a false value for all pairs of false values.
A true value returns only for arguments, one of which is assigned a value of NULL, and the second is not assigned any value.
Difference of variables with NULL value and undefined variables
The
===
operator allows you to distinguish all false values, except for variables with a NULL value from variables that were not assigned a value.
Such variables can be distinguished using the
get_defined_vars () function.
If it is necessary to determine whether the value of the
$var
variable has been assigned, then the following code fragment can be used for this:
if (array_key_exists('var', get_defined_vars()) ) { echo "var is defined";
findings
You should always remember that in PHP two false values may not be equal to each other, and variables that, at first glance, are different, may turn out to be the same when compared. To avoid such surprises, you can use
===
and
!==
operators.
When working with arrays, in order to avoid surprises, you can write a function for converting values into guaranteed different indices. After that, the elements of the array should be treated only with its help. This may slow down the program, but will help to avoid surprises.
Testing was conducted on PHP 5.3.1.
Additional links
1.
PHP type comparison tables .
2.
MySQl Null and Empty Strings in context of PHP .