Re: No notices for undefined index

Re: No notices for undefined index

am 08.04.2010 19:36:00 von Ashley Sheridan

--=-MmfWUiO/m7eSP1LpKRrt
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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



--=-MmfWUiO/m7eSP1LpKRrt--

No notices for undefined index

am 08.04.2010 19:36:21 von Shawn McKenzie

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: No notices for undefined index

am 08.04.2010 19:45:24 von Andre Polykanine

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?
--
With best regards from Ukraine,
Andre
Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org
Yahoo! messenger: andre.polykanine; ICQ: 191749952
Twitter: m_elensule

----- Original message -----
From: Shawn McKenzie
To: php-general@lists.php.net
Date: Thursday, April 8, 2010, 8:36:21 PM
Subject: [PHP] No notices for undefined index

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: No notices for undefined index

am 08.04.2010 20:21:20 von Bob McConnell

In the first case, $a=3D5 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

-----Original Message-----
From: Andre Polykanine [mailto:andre@oire.org]=20
Sent: Thursday, April 08, 2010 1:45 PM
To: Shawn McKenzie
Cc: php-general@lists.php.net
Subject: Re: [PHP] No notices for undefined index

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?
--=20
With best regards from Ukraine,
Andre
Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @
jabber.org
Yahoo! messenger: andre.polykanine; ICQ: 191749952
Twitter: m_elensule

----- Original message -----
From: Shawn McKenzie
To: php-general@lists.php.net
Date: Thursday, April 8, 2010, 8:36:21 PM
Subject: [PHP] No notices for undefined index

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 =3D 5;
print $a[1];
print $a['test'];

$a =3D array();
print $a[1];
print $a['test'];

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

--=20
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--=20
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: No notices for undefined index

am 08.04.2010 22:03:30 von Shawn McKenzie

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: No notices for undefined index

am 08.04.2010 22:09:11 von Shawn McKenzie

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: No notices for undefined index

am 08.04.2010 22:22:48 von Shawn McKenzie

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: No notices for undefined index

am 09.04.2010 00:39:37 von Ashley Sheridan

--=-vQKZWYiO3brSzSQk+kPZ
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Thu, 2010-04-08 at 15:22 -0500, Shawn McKenzie wrote:

> 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
>


I think it just returns null if the offset goes beyond the length of the
string. In C and C++ doing something like this would take you beyond
that variables memory allocation into neighbouring variables. I believe
PHP is trying to prevent problems where that might occur by returning
null instead.

This is only conjecture as I don't know exactly what happens, and I
can't find anything in the manual that explains what should happen when
you treat a string like an array in PHP. However, throwing some sort of
error or notice would be nice, but could be worked around by checking
the array type and length (the count() function returns the string
length I believe as well as the array size) as it does seem that PHP is
treating a string like a special sort of array.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-vQKZWYiO3brSzSQk+kPZ--

Re: No notices for undefined index

am 09.04.2010 04:22:06 von kranthi

>> print $a[0]; // prints 5
>> print $a[100]; // Notice: Uninitialized string offset: 100
Yup, this should happen when 5 is treated as an array of characters.
In other words as a string.
$a = '5';
echo $a[0];
echo $a[100];
gives you the expected result

regarding the original question, i think that the interpreter is
prefilling the variable with null

$a = 5;
var_dump(isset($a[0]));
var_dump($a[0]);

since $a[0] is already assigned (to null) the interpreter is not
throwing a notice

$b = null;
var_dump(isset($b));
var_dump($b);

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: No notices for undefined index

am 09.04.2010 05:28:29 von hSiplu

Hello Shawn,
Why dont you report a bug? When we know the expected behavior or the
way it SHOULD behave. and its not behaving that way. Its certainly a
bug.. Only then we can know the real reason why the novicas are not
showing up.

On 4/8/10, 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
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
Sent from my mobile device

Shiplu Mokaddim
My talks, http://talk.cmyweb.net
Follow me, http://twitter.com/shiplu
SUST Programmers, http://groups.google.com/group/p2psust
Innovation distinguishes bet ... ... (ask Steve Jobs the rest)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: No notices for undefined index

am 09.04.2010 11:08:33 von Ashley Sheridan

--=-1lK433CUZ5aWaXjbRfDS
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Fri, 2010-04-09 at 07:52 +0530, kranthi wrote:

> >> print $a[0]; // prints 5
> >> print $a[100]; // Notice: Uninitialized string offset: 100
> Yup, this should happen when 5 is treated as an array of characters.
> In other words as a string.
> $a = '5';
> echo $a[0];
> echo $a[100];
> gives you the expected result
>
> regarding the original question, i think that the interpreter is
> prefilling the variable with null
>
> $a = 5;
> var_dump(isset($a[0]));
> var_dump($a[0]);
>
> since $a[0] is already assigned (to null) the interpreter is not
> throwing a notice
>
> $b = null;
> var_dump(isset($b));
> var_dump($b);
>


If $a is treated as an array of characters, then $a[0] is always the
first character, not null.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-1lK433CUZ5aWaXjbRfDS--

RE: No notices for undefined index

am 09.04.2010 14:15:53 von Bob McConnell

From: Shawn McKenzie

> Bob McConnell wrote:
>> In the first case, $a=3D5 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.
>>=20
>> 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.
>>=20
>> Such are the joys of loosely typed languages.
>=20
> Yes, this is what I was thinking as well, however:
>=20
> $a=3D5;
> 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?

I'm assuming that the PHP interpreter works much like a C compiler. i.e.
It doesn't keep track of the size of strings. It knows that $a maps to a
memory location, and $a[100] maps to that location plus 100 characters.
As long as that is still a valid memory address for this process, it
doesn't see anything wrong. If it is outside the process memory, you are
more likely to get a General Protection Fault, or the equivalent OS
error.

In security parlance, this is what is known as a buffer overflow error.
The application programmer is responsible for keeping track of string
sizes and insuring that indexes don't move past the end of the allocated
space. It is also why functions like snprintf should be used instead of
sprintf.

Bob McConnell

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: No notices for undefined index

am 09.04.2010 15:15:02 von Nathan Rixham

Ashley Sheridan wrote:
> can't find anything in the manual that explains what should happen when
> you treat a string like an array in PHP.

http://www.php.net/manual/en/language.types.string.php#langu age.types.string.substr

:)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: No notices for undefined index

am 09.04.2010 21:36:16 von Ashley Sheridan

--=-YZ4A1U/uwmT5pMU9eEAH
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Fri, 2010-04-09 at 14:15 +0100, Nathan Rixham wrote:

> Ashley Sheridan wrote:
> > can't find anything in the manual that explains what should happen when
> > you treat a string like an array in PHP.
>
> http://www.php.net/manual/en/language.types.string.php#langu age.types.string.substr
>
> :)
>


Thanks, and I think that page answers the whole question:

"Writing to an out of range offset pads the string with spaces.
Non-integer types are converted to integer. Illegal offset type emits
E_NOTICE. Negative offset emits E_NOTICE in write but reads empty
string. Only the first character of an assigned string is used.
Assigning empty string assigns NUL byte."

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-YZ4A1U/uwmT5pMU9eEAH--