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

- Thread: https://externals.io/message/21827
- Original archive: https://news-web.php.net/php.internals/21827
- Started by Ian P. Christian on 2006-02-09T21:54:01+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #21827 — Ian P. Christian — 2006-02-09T21:54:01+00:00

URL: https://externals.io/message/21827#21827

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

---

## #21831 — Evan Priestley — 2006-02-09T22:06:08+00:00

URL: https://externals.io/message/21827#21831 · In reply to: #21827

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

---

## #21828 — Sean Coates — 2006-02-09T22:08:52+00:00

URL: https://externals.io/message/21827#21828 · In reply to: #21827

> 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

---

## #21829 — Ian P. Christian — 2006-02-09T22:10:06+00:00

URL: https://externals.io/message/21827#21829 · In reply to: #21828

> 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

---

## #21830 — Sean Coates — 2006-02-09T22:15:45+00:00

URL: https://externals.io/message/21827#21830 · In reply to: #21829

> 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

---

## #21832 — Robert Cummings — 2006-02-09T22:28:40+00:00

URL: https://externals.io/message/21827#21832 · In reply to: #21829

> 
> > […]
> 
> 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.          |
`------------------------------------------------------------'

---

## #21833 — Ian P. Christian — 2006-02-09T22:43:56+00:00

URL: https://externals.io/message/21827#21833 · In reply to: #21832

> 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
