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]

 



Hi Aziz,

Problem is i didn't switch that.

The test i mentioned here: http://news.php.net/php.general/323297
showing this result with PHP 5.5.


On Thu, May 15, 2014 at 9:07 AM, Aziz Saleh <azizsaleh@xxxxxxxxx> wrote:
>
>
>
> On Wed, May 14, 2014 at 8:55 PM, Rain Lee <root@xxxxxxx> wrote:
>>
>> So, here is the TLDR;
>>
>> $string = 'this is a string';
>>
>> In PHP 5.5
>> isset($string['check']) === false
>> isset($string['check'][0]) === true
>>
>> Why can't let
>> isset($string['check']) === false
>> isset($string['check'][0]) === false <--
>>
>> ?
>>
>> On Thu, May 15, 2014 at 12:27 AM, Jim Lucas <lists@xxxxxxxxx> wrote:
>> > On 05/13/2014 08:22 AM, \R\N wrote:
>> >>
>> >> Greeting guys,
>> >>
>> >> I got a problem on checking and getting a string with multi dim index
>> >> (like
>> >> ['blablabla']['blabla']). It confusing me, so I think I need to found
>> >> an
>> >> answer to clear myself.
>> >>
>> >> First of all, I known that is wrong to get something from a string
>> >> using a
>> >> multi dim index or even a string index. I did it by mistakenly set a
>> >> wrong
>> >> parameter. The parameter supposed to be array with string as index
>> >> (That
>> >> will actually be a hashmap), but I setted a string on it.
>> >>
>> >> As result, I got a warning raised: Illegal string offset.
>> >>
>> >> It's normal since I use the string as index to attempt to getting
>> >> something
>> >> from a string. But the strange thing is, I did the isset check before
>> >> use
>> >> the index. It supposed to be hot pass the check, but it did.
>> >>
>> >> So I made a shorter test here:
>> >>
>> >>
>> >> https://gist.github.com/raincious/682ba152dbd74537e323#file-stringisset-php
>> >>
>> >> The test 1 is what I used in my application, it checks if the array
>> >> index
>> >> is
>> >> set and not empty in same time. A valid param for it should be like
>> >> this:
>> >>
>> >> $string = array(
>> >>      'check' => 'yes',
>> >> ); // Please ignore the variable name
>> >>
>> >> But when the $string truly turned to a string, it become the source of
>> >> my
>> >> question.
>> >>
>> >> Please notice that, on document:
>> >> http://www.php.net/manual/en/function.isset.php says, "Checking
>> >> non-numeric
>> >> offsets of strings now returns FALSE." since 5.4, and I using 5.5.
>> >>
>> >> So the test 3 (isset($string['check']) one) running as expected, it
>> >> returns
>> >> false. However, the test1 (isset($string['check'][0]) one) is
>> >> unfortunatly
>> >> not, It actually returnning true.
>> >>
>> >>
>> >> I'm not a experter of PHP and I even need other people's help to read
>> >> PHP
>> >> source code, but in the information from the document and the talking
>> >> with
>> >> other people gives me some guts to guess the $string['check'][0]
>> >> actually
>> >> will be converted into $string[(int)'check'][0] and then into
>> >> $string[0][0],
>> >> so the test 1 can be pass. Is that right?
>> >>
>> >> So, what I want to know is, why there has two kind of behaviour for
>> >> isset?
>> >> Maybe it's better to let the isset($string['check'][0]) and
>> >> isset($string['check']) both return false?
>> >>
>> >
>> > What you are describing, and what is in your test script on github is
>> > extremely confusing.  I'm still not sure what you think you are trying
>> > to
>> > accomplish with your tests.
>> >
>> > Can you give us a real world example of what it is you are trying to do?
>> >
>> >
>> >>
>> >> And sorry for my english, please ask if you confused by me.
>> >>
>> >> Thank you,
>> >> R
>> >
>> >
>> >
>> > --
>> > Jim Lucas
>> >
>> > http://www.cmsws.com/
>> > http://www.cmsws.com/examples/
>> >
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>
> I think you switched the two:
>
> isset($string['check']) === true
>
> isset($string['check'][0]) === false
>
> 'check' is automatically converted to an int by PHP (since the array is
> numeric) which gives you $string[0] which is set. However, if you try ding
> $string[0][0] ($string['check'][0]) it will give you false since it does not
> exist.
>
> PHP is pretty flexible when it comes to using ints as strings and vice
> verse, you just need to know you are using them correctly.
>
> Also use var_dump() when doing checks to give you a better idea what PHP is
> doing behind the scenes.
>
> Aziz

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