duangu4943 2018-09-12 12:14
浏览 11

当未设置其中一个元素时,表达式返回true。 对象数组

if(isset($cat[$k]->id) && $cat[$k]->id==$nav[$lvl-1]->id) // = false 

But

if($cat[$k]->id==$nav[$lvl-1]->id) // = true

How is it possible?

  • 写回答

2条回答 默认 最新

  • dongle7553 2018-09-12 12:33
    关注

    Your code is probably right, but your statements are returning false.

    You need to debug it.

    I will write you an example on how to do this, including documentation.

    Example:

    $test1 = false;
    $test2 = null;
    $test3 = [];
    $test4 = 'asd';
    $test5 = 1;
    
    if ($test1)
    {
        echo 'Valid';
    }
    else
    {
        echo 'False';
    }
    // Output: 'False'
    // This is because $test1 = false. The if statement will check if $test1 is set/true and not false.
    
    if ( ! $test2)
    {
        echo 'NULL';
    }
    else
    {
        echo 'NOT NULL';
    }
    // Output: 'NULL'
    // $test2 is NULL/EMPTY (NULL = not valid)
    // For the if( ! ..) part, will say if not.
    
    if (is_array($test3))
    {
        echo 'Is array';
    }
    else
    {
        echo 'Is not an array';
    }
    // Output: 'Is array'
    // [] is short for array(); $test3 is an array, so your if statement will continue as valid.
    
    if ( ! empty($test4) || is_integer($test5))
    {
        echo 'Valid';
    }
    else
    {
        echo 'Is not valid';
    }
    // Output: 'Valid'
    // Both $test4 and $test5 pass the if statement. Because $test4 is not empty OR $test5 is an integer.
    

    Related to your code:

    echo '<pre>';
    var_dump($cat[$k]);
    echo '</pre>';
    die;
    
    if(isset($cat[$k]->id) && $cat[$k]->id==$nav[$lvl-1]->id);
    

    You need to 'debug' your code. You require $cat[$k]->id to be set, if it is not set it will return false.

    While debugging you check if the data is parsed correctly or not.

    Documentation:


    Instead of giving you the right answer right away, I wish to show you how to debug your code and to understand how isset() works. In case you have question, just let me know in a comment.

    Good luck!

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动