'odd' handling of $moo['bar'] where $moo is a string

php.internals

Ian P. Christian

20 years ago
Can someone explain the following to me? echo "DEBUG:\n"; var_dump($keys); var_dump($keys['file']); var_dump(isset($keys['NOTEXISTING'])); var_dump(isset($keys['file'])); exit; DEBUG: string(19) "myConsoleController" string(1) "m" bool(true) bool(true) Now, it seems that $keys['file'] == $keys[0], which makes sense why the issets return true. However... if ('file') echo 'true'; will print moo, therefore 'file' == 1, not 0. Why is this different when using it as a string offset? IMO, using a string as a string offset, a fatal error should be raised. Kind Regards,
-- Ian P. Christian http://pookey.co.uk

Sean Coates

20 years ago
> Now, it seems that $keys['file'] == $keys[0], which makes sense why the issets > return true. However... > if ('file') echo 'true'; > will print moo, therefore 'file' == 1, not 0. Why is this different when using > it as a string offset? > IMO, using a string as a string offset, a fatal error should be raised.
It's cast to an integer. sean@iconoclast:~$ php -r 'echo (int) "file" . "\n";' 0 sean@iconoclast:~$ php -r 'echo (bool) "file" . "\n";' 1 S

Ian P. Christian

20 years ago
On Thursday 09 February 2006 22:08, Sean Coates wrote:
> It's cast to an integer.
Ahh.. I see. That does make sense now as to why it results in the first char in the array - however I still think it makes more sense not to cast, but rather to give a fatal error. Thanks,
-- Ian P. Christian http://pookey.co.uk

Sean Coates

20 years ago
> Ahh.. I see. That does make sense now as to why it results in the first char > in the array - however I still think it makes more sense not to cast, but > rather to give a fatal error.
PHP autocasts everywhere. This would break years worth (and thousands) of applications. S

Robert Cummings

20 years ago
On Thu, 2006-02-09 at 17:10, Ian P. Christian wrote:
> On Thursday 09 February 2006 22:08, Sean Coates wrote: > > It's cast to an integer. > > Ahh.. I see. That does make sense now as to why it results in the first char > in the array - however I still think it makes more sense not to cast, but > rather to give a fatal error.
PHP is primarily a web language. Many indexes are pulled from $_GET and $_POST and for obvious reasons are strings. One of the convenience factors of PHP is that noobs don't need to think about casting their strings to ints to index. Cheers, Rob.
-- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

Ian P. Christian

20 years ago
On Thursday 09 February 2006 22:28, Robert Cummings wrote:
> PHP is primarily a web language. Many indexes are pulled from $_GET and > $_POST and for obvious reasons are strings. One of the convenience > factors of PHP is that noobs don't need to think about casting their > strings to ints to index.
Yeah ok, good point. The strenghts of PHP sometimes bite you in the arse ;) Thanks for your replies people.
-- Ian P. Christian http://pookey.co.uk

Evan Priestley

20 years ago
When indexing a string, the string is cast to an integer: (int)'file' == 0 When in a conditional expression, the string is cast to a boolean: (bool)'file' == true It isn't necessarily a fatal error, consider: $keys[ '3' ] - Evan On Feb 9, 2006, at 4:54 PM, Ian P. Christian wrote: