$tally['zero']['status']='hello';
echo $tally['zero']['status'];
//prints hello, this is expected
In this example, why is only the first letter being printed?
$tally = array( "zero" => '0');
$tally['zero']['status']='hello';
echo $tally['zero']['status'];
// prints h, I was expecting hello
In this example, why is an error being thrown?
$tally['zero'] = 0;
$tally['zero']['status']='hello';
echo $tally['zero']['status'];
//prints Warning: Cannot use a scalar value as an array
In PHP, strings can be indexed like arrays, and can also be modified in place. So
'status'becomes0when indexing the string, and the first character ofhellois assigned to the first letter of$tally['zero']. For example, this:Would print "h1".
Like the error says,
0is not an array. You can’t index it, hence the warning.