Re: A little confusing thing on (mistakenly) geting a offset from string with multi dim index.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxxxxxxxxx...


> -----Original Message-----
> From: Rain Lee [mailto:root@xxxxxxx]
> Sent: 15 May 2014 01:55
> To: Jim Lucas
> Cc: php-general@xxxxxxxxxxxxx
> Subject: Re:  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.



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





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux