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]

 



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


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@xxxxxxxxxxxxxx     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





[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