[英]php bool echo output
我有一個奇怪的問題。
如果有這樣做的函數返回值,
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
為什么?
從PHP文檔:
布爾值TRUE值轉換為字符串“1”。 Boolean FALSE轉換為“”(空字符串)。 這允許在布爾值和字符串值之間來回轉換。
http://www.php.net/manual/en/language.types.string.php
要使用類型打印返回值,請嘗試使用var_dump($myVar)
。
將布爾值轉換為字符串時, true
轉換為"1"
, false
轉換為""
(空字符串)。
http://php.net/manual/en/language.types.string.php#language.types.string.casting
你的期望是不正確的。
這就是PHP在將bool轉換為字符串時所做的事情。
嘗試這個:
echo $collection->isVipCustomer($customer_id) ? 1 : 0;
或者,如果您只是想輸出一些用於調試目的而且想要有明確的錯誤標志,請使用:
var_dump($collection->isVipCustomer($customer_id));
這將輸出bool(true)
或bool(false)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.