From: Shawn McKenzie on
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
From: Ashley Sheridan on
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


From: Shawn McKenzie on
Andre Polykanine wrote:
> Hello Shawn,
>
> Hm... isn't it expected behavior? Since you haven't defined a
> $a['test'] item, PHP throws a notice... or I'm wrong?

Yes it is expected. I'm saying the opposite that it doesn't in the
first case.

--
Thanks!
-Shawn
http://www.spidean.com
From: Shawn McKenzie on
Bob McConnell wrote:
> In the first case, $a=5 creates a multi-typed variable. The interpreter
> makes its best guess how the next two expressions should be interpreted.
> In both cases, they look a lot like an index into a character array
> (string), and 'test' evaluates numerically to zero. Both are valid
> offsets for a string, so no messages are generated.
>
> In the second case, $a is explicitly declared as an array. This give the
> interpreter a lot more detail to work from. The two expressions are now
> an index and a key for the array. But both of them evaluate to offsets
> that have not been assigned, which raises a flag and creates the
> warnings.
>
> Such are the joys of loosely typed languages.
>
> Bob McConnell

Yes, this is what I was thinking as well, however:

$a=5;
print $a[0]; // if it is index 0 then it should print 5 yes?
print $a[100]; // there is no index 100 so why no notice?

--
Thanks!
-Shawn
http://www.spidean.com
From: Shawn McKenzie on
Shawn McKenzie wrote:
> Bob McConnell wrote:
>> In the first case, $a=5 creates a multi-typed variable. The interpreter
>> makes its best guess how the next two expressions should be interpreted.
>> In both cases, they look a lot like an index into a character array
>> (string), and 'test' evaluates numerically to zero. Both are valid
>> offsets for a string, so no messages are generated.
>>
>> In the second case, $a is explicitly declared as an array. This give the
>> interpreter a lot more detail to work from. The two expressions are now
>> an index and a key for the array. But both of them evaluate to offsets
>> that have not been assigned, which raises a flag and creates the
>> warnings.
>>
>> Such are the joys of loosely typed languages.
>>
>> Bob McConnell
>
> Yes, this is what I was thinking as well, however:
>
> $a=5;
> print $a[0]; // if it is index 0 then it should print 5 yes?
> print $a[100]; // there is no index 100 so why no notice?
>

$a='5';
print $a[0]; // prints 5
print $a[100]; // Notice: Uninitialized string offset: 100

So it seems, in the first case with the integer 5 that the interpreter
is saying:

- Since $a is not an array I'll treat $a[0] and $a[100] as a string
offset, but since $a is not a string I won't do anything.

Just seems stupid IMHO.

--
Thanks!
-Shawn
http://www.spidean.com