<?php
/* PHP devs, test & tell me I'm crazy. */
$x[] = '1';
if (empty($x[0]['x'])) {
echo 'No PHP bug.';
}
else {
echo 'PHP bug exists.';
}
?>
I always get “PHP bug exists.”
<?php
/* PHP devs, test & tell me I'm crazy. */
$x[] = 1;
if (empty($x[0]['x'])) {
echo 'No PHP bug.';
}
else {
echo 'PHP bug exists.';
}
?>
Outputs “No PHP bug.”
<?php
/* PHP devs, test & tell me I'm crazy. */
$x[] = '1';
if (!isset($x[0]['x'])) {
echo 'No PHP bug.';
}
else {
echo 'PHP bug exists.';
}
?>
Outputs “PHP bug exists.”
<?php
/* PHP devs, test & tell me I'm crazy. */
$x[] = '1';
if (!isset($x[0]['hello world'])) {
echo 'No PHP bug.';
}
else {
echo 'PHP bug exists.';
}
?>
Outputs “PHP bug exists.”
This is because you are assigning a string to the array. Because of that, the
xin$x[0]['x']gets auto-cast into0– remember, the second indes is not pointing to an array, but a string, which can’t have non-numeric indexes.$x[0][0]indeed exists – it’s the first character of the string.$x[0][1]does not exist, and your test returns the correct result if you change the index accordingly:Lesson: Even though strings can be accessed like arrays, they aren’t arrays.
There’s a warning in the PHP manual page on strings explaining the behaviour: