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 Christoph Becker,

Thank you for reply this topic.

Yes, i know to use array_key_exists, i believe i got the point.

I don't use it because it slower than isset, and isset is still needed
for checking if the variable's existing. Now i use is_array to check
if i'm testing a array for isset, so the code will be something like:

if (isset($val['key'][0]) && is_array($val)) {

and the full check will need an is_string to make sure the $val['key']
is a string. So it becomes

if (isset($val['key'][0]) && is_array($val) && is_string($val['key'])) {

The array_key_exists not making the code simpler, it will looks like:

if (isset($val) && array_key_exists('key', $val) &&
is_string($val['key']) && !empty($val['key'])) {

Or the code in document comment (with modification):
if ((isset($val['key']) || array_key_exists('key', $val)) &&
is_string($val['key']) && !empty($val['key'])) {


However, if isset($string['check'][0]) and isset($string['check']) can
both be false, i just need to do:

if (isset($val['key'][0]) && is_string($val['key'])) {

It's saving life and time. And i think it's intuitively making sense,
because the string offset 'key' shouldn't be exist for a string,
therefore the ['key'][0] should not be exist too.

I think the convert that cause the 'key' become 0 is ... not a good
idea. But my experience on PHP is so little, maybe there is some
reason for it that i never know.

Don't know what's the opinion of those guys inside the PHP.


On Wed, May 14, 2014 at 1:56 AM, Christoph Becker <cmbecker69@xxxxxx> wrote:
> \R\N wrote:
>
>> 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?
>
> Yes.
>
>> 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?
>
> The behavior of isset is fine.  Consider that $string['check'] is
> treated as $string[0], so it evaluates to a string with only the first
> character of $string.  Then it is checked, whether this string has a
> character at position zero.  As this is true, isset returns true.
>
> You may consider using array_key_exists() instead of isset().
>
> --
> Christoph M. Becker
>

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