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
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
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
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
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
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
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. |
`------------------------------------------------------------'
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