2015-01-08 40 views
-1

hei,所以我在這裏要做的是驗證我的函數的兩個來源,如果設置$ this-> post [「search」]使用它或者如果設置$這 - >發佈[「身份證」]使用ID,但不能同時:使用邏輯運算符'或'返回總是真的

($this->post["search"] or $this->post["id"]) 

如果我甩我得到真正的答案一個布爾值,爲什麼呢? 所以,如果我有一個很長的字符串像SQL字符串我不希望使用elseif「SI希望更多這樣的:

$this->sql->cell("select search_for('" . $this->post["search"] or $this->post["id"] . "') as response;"); 
+0

($ this-> post [「search」]或$ this-> post [「id」])返回_true_或_false_,而不是數組後面elements.i.e的內容。如果$ this-> post [「search」]包含'find me',那麼它是_true_。 –

+0

我想在問題中使用一行,但似乎我需要使用if/else –

+1

_($ this-> post [「search」]?$ this-> post [「search」]:$ this - > post [「id」])_將返回任一值,如果至少有一個被提供。 [使用PHP三元運營商](http://www.sitepoint.com/using-the-ternary-operator/) –

回答

0

你或許應該根據什麼設定通過控制流:

if (isset($this->post["search"])) { 
    // deal with search 
} else if (isset($this->post["id"])) { 
    // deal with id 
} else { 
    // gracefully reject dealing: nothing to do 
} 

BTW,isset回報true爲空字符串:

$test = array('a' => ''); 
var_dump(isset($test['a'])); 
//⇒ bool(true) 

要確定是否$_POST參數有一個值,用途:

!empty($_POST['id']) 

希望它有幫助。

+0

我修改我的問題,請!所以我不想使用elseif的我想知道爲什麼如果我轉儲行$ this-> post [「search」]或$ this-> post [「id」]我總是真的嗎? –

+0

@JohniDevo請參閱更新。 – mudasobwa