On Thu, 2010-04-08 at 12:36 -0500, Shawn McKenzie wrote:
> So the first two print statements generate NO notices, while the second
> obviously generates:
>
> Notice: Undefined offset: 1 in /home/shawn/www/test.php on line 11
>
> Notice: Undefined index: test in /home/shawn/www/test.php on line 12
>
> This sucks. A bug???
>
> error_reporting(E_ALL);
> ini_set('display_errors', '1');
>
>
> $a = 5;
> print $a[1];
> print $a['test'];
>
> $a = array();
> print $a[1];
> print $a['test'];
>
> --
> Thanks!
> -Shawn
> http://www.spidean.com
>
I think this goes back to the C style strings, where a string is just a
collection of characters. I've noticed that in PHP you can treat a
string as if it were an array of characters, so I guess in both cases
above, it would be trying to return the second character, which is the
termination character or a chr(0).
In the second example, you've explicitely declared $a to be an array, so
PHP creates a proper index for it, and then when you ask for an element
that is not in that index list, it throws a notice at you.
Thanks,
Ash
http://www.ashleysheridan.co.uk