File tree 1 file changed +33
-2
lines changed
1 file changed +33
-2
lines changed Original file line number Diff line number Diff line change 29
29
No warning is generated if the variable does not exist.
30
30
That means <function >empty</function > is essentially the
31
31
concise equivalent to <command >!isset($var) || $var == false</command >.
32
+ This also applies to nested structures, such as a multidimensional array or chained properties.
32
33
</para >
33
34
</listitem >
34
35
</varlistentry >
@@ -79,7 +80,6 @@ $expected_array_got_string = 'somestring';
79
80
var_dump(empty($expected_array_got_string['some_key']));
80
81
var_dump(empty($expected_array_got_string[0]));
81
82
var_dump(empty($expected_array_got_string['0']));
82
- var_dump(empty($expected_array_got_string[0.5]));
83
83
var_dump(empty($expected_array_got_string['0.5']));
84
84
var_dump(empty($expected_array_got_string['0 Mostel']));
85
85
?>
@@ -91,9 +91,40 @@ var_dump(empty($expected_array_got_string['0 Mostel']));
91
91
bool(true)
92
92
bool(false)
93
93
bool(false)
94
- bool(false)
95
94
bool(true)
96
95
bool(true)
96
+ ]]>
97
+ </screen >
98
+ </example >
99
+ <example >
100
+ <title ><function >empty</function > on multidimensional arrays</title >
101
+ <programlisting role =" php" >
102
+ <![CDATA[
103
+ <?php
104
+ $multidimensional = [
105
+ 'some' => [
106
+ 'deep' => [
107
+ 'nested' => 'value'
108
+ ]
109
+ ]
110
+ ];
111
+
112
+ if (!empty($multidimensional['some']['some']['nested'])) {
113
+ $someVariable = $multidimensional['some']['deep']['nested'];
114
+ }
115
+
116
+ var_dump(empty($multidimensional['some-undefined-key']));
117
+ var_dump(empty($multidimensional['some']['deep']['unknown']));
118
+ var_dump(empty($multidimensional['some']['deep']['nested']));
119
+ ?>
120
+ ]]>
121
+ </programlisting >
122
+ &example.outputs;
123
+ <screen >
124
+ <![CDATA[
125
+ bool(true)
126
+ bool(true)
127
+ bool(false)
97
128
]]>
98
129
</screen >
99
130
</example >
You can’t perform that action at this time.
0 commit comments