2013-04-21 40 views
0

enter image description here條件來檢查了一下現場

...我正在檢查該字段像這樣:

if((bool)$website['IsDeleted']) { }

但不管它總是返回在MySQL中值的空字符串現場閹其01

["IsDeleted"]=> string(1) "" }

請告訴我我在這裏做什麼? if條件是否應該修改?

+0

是不是'if($ website ['IsDeleted']){}'? – Amir 2013-04-21 06:59:34

+0

@Amir - 他將變量$ website ['IsDeleted']轉換爲布爾值 - 這沒什麼問題。 – bestprogrammerintheworld 2013-04-21 07:24:06

+0

@Nimbuz那麼你的意思是它總是執行'else'語句,無論它是0還是1? – Amir 2013-04-21 08:12:58

回答

0

我看不出你的if語句有什麼問題。

<?php 
$website['IsDeleted'] = 1; 
if((bool)$website['IsDeleted']) { echo 'found you';} 
?> 

將輸出 找到你

<?php 
$website['IsDeleted'] = 1; 
if((bool)$website['IsDeleted']) { echo $website['IsDeleted'];} 
?> 

將輸出1

如果值爲NULL,它雖然不會工作。然後你將不得不檢查NULL值。

<?php 
$website['IsDeleted'] = null; 
if((bool)$website['IsDeleted'] || $website['IsDeleted'] === null) { echo 'found you';} 
?> 

會輸出找到你。

+0

我無法手動設置該值。必須從數據庫中提取並檢查。 – 3zzy 2013-04-21 06:40:42

+0

看起來從db中獲取的值是實際的問題,然後(而不是if語句)。你如何做抓取? – bestprogrammerintheworld 2013-04-21 06:48:44