1

我有一个奇怪的问题。

如果有执行此操作的函数返回值,

public function isVipCustomer($customer_id){
        $cus_vip_id=FALSE;
        $sql="SELECT customerid FROM customers WHERE is_vip='1' 
                AND customerid='".$customer_id."' LIMIT 0,1 ";  
        $result= $this->db->query($sql);

        while($row=mysqli_fetch_assoc($result)){
            $cus_vip_id=(int)$row['customerid'];    
        }

        if($cus_vip_id!=0)
            return TRUE;
        else
            return FALSE;

    }

当我打电话时

$customer_id=13;
echo $collection->isVipCustomer($customer_id);

当它为真时,它输出 1,但当它为假时,它为空,期望输出 0

为什么?

4

3 回答 3

3

来自 PHP 文档:

布尔 TRUE 值转换为字符串“1”。Boolean FALSE 被转换为“”(空字符串)。这允许在布尔值和字符串值之间来回转换。

http://www.php.net/manual/en/language.types.string.php

要使用类型打印返回值,请尝试使用var_dump($myVar)

于 2012-04-18T05:49:55.503 回答
1

将布尔值转换为字符串时,true转换为"1"和转换false""(空字符串)。

http://php.net/manual/en/language.types.string.php#language.types.string.casting

你的期望是不正确的。

于 2012-04-18T05:47:33.780 回答
0

这就是 PHP 在将 bool 转换为字符串时所做的。
尝试这个:

echo $collection->isVipCustomer($customer_id) ? 1 : 0;

或者,如果您只是想输出一些用于调试目的的内容,并且想要明确表示错误,请使用以下命令:

var_dump($collection->isVipCustomer($customer_id));

这将输出bool(true)bool(false)

于 2012-04-18T05:48:11.620 回答