为什么isset和!isset都返回true?

时间:2014-04-08 13:21:35

标签: php

以下代码块为数组中的每个元素打印FOUND和NOT FOUND。我错过了什么?

$values = DB::query($SQL);

print_r($values);
foreach($values as $value)
{
    $myVal = $value->thisAttribute;

    if(isset($myVal)) print("FOUND");

    if(!isset($myVal)) print("NOT FOUND");
}

输出:

Array
(
    [0] => stdClass Object
        (
            [thisAttribute] => 
        )

    [1] => stdClass Object
        (
            [thisAttribute] => value
        )

    [2] => stdClass Object
        (
            [thisAttribute] => 
        )
)    
FOUNDNOTFOUNDFOUNDNOTFOUNDFOUNDNOTFOUND

1 个答案:

答案 0 :(得分:5)

我想这样做的正确方法是:

foreach($values as $value)
{
  if(isset($value->thisAttribute)) {
    $myVal = $value->thisAttribute;
    print("FOUND");
  } else
    print("NOT FOUND");
}