RE: [PHP] A little confusing thing on (mistakenly) geting a offset from string with multi dim index.
| From: | Ford, Mike | Date: | Thu, 15 May 2014 16:39:32 +0000 |
| Subject: | RE: [PHP] A little confusing thing on (mistakenly) geting a offset from string with multi dim index. | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-323332@lists.php.net to get a copy of this message | ||
> -----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.
Cheers!
Mike
--
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
403a Leslie Silver Building, City Campus, Leeds Metropolitan University,
Woodhouse Lane, LEEDS, LS1 3ES, United Kingdom
E: m.ford@leedsmet.ac.uk T: +44 113 812 4730
From 22 September 2014 Leeds Metropolitan University will become Leeds Beckett University.
Find out more at http://www.leedsbeckett.ac.uk
To view the terms under which this email is distributed, please go to:-
http://www.leedsmet.ac.uk/email-disclaimer.htm