Re: A little confusing thing on (mistakenly) geting a offset from string with multi dim index.
| From: | \\R\\N | Date: | Fri, 16 May 2014 01:23:40 +0000 |
| Subject: | Re: A little confusing thing on (mistakenly) geting a offset from string with multi dim index. | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-323336@lists.php.net to get a copy of this message | ||
Thank you Mike.
I post this thread just because I surprised by this behavior (Not because I
got problem on it, so there is no real world example, just the test itself).
In simple say, I don't think the conversion is good idea. I mean, if
isset($string['check']) and isset($string['check'][0]) can be both false,
that will be better for understanding. (So I post my complain and looking
for chances of change)
I did post in php.internals, but no response yet, maybe thats because my
english too poor to explain this clearly or guys inside php think that is
fine.
isset($string['check'], $string['check'][0]) is good, I will use it when I
need.
""Ford, Mike""
news:3158105795AAC1408619EC8BA03384A93CFF73E8@EXC-PUB01A.leedsmet.ac.uk...
> -----Original Message-----
> From: Rain Lee [mailto:root@3ax.org]
> Sent: 15 May 2014 01:55
> To: Jim Lucas
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] A little confusing thing on (mistakenly) geting a
> offset from string with multi dim index.
>
> So, here is the TLDR;
>
> $string = 'this is a string';
>
> In PHP 5.5
> isset($string['check']) === false
Yes, because isset is specifically defined (nowadays) to reject string
Offsets on a string.
> isset($string['check'][0]) === true
This is correct because the isset is only checking the final index -
it's not doing any checks on 'check', so the normal PHP rules of
string->integer conversion apply, and you get a zero. (If you don't
know what the "normal rules" are, see http://ow.ly/wT2t0).
So, $string['check'] is effectively $string[0], which is "t"; so
$string['check'][0] is $string[0][0], which is set and evaluates
to "t".
To check that all the indexes work, you will need to do
if (isset($string['check']) && isset($string['check'][0]))
Or even
if (isset($string['check'], $string['check'][0]))
which has the same effect.