2013-01-24 96 views
0

由於某種奇怪的原因,我無法調試這爲我的生活.. $ this-> input-> post('post_page')= 10 ....我回顯變量並且它在屏幕上打印10。 當它應該是真的時,這會一直返回假。 所以...有人可以幫我解決這個問題嗎?我試圖在每個檢查周圍放置單獨的括號並更改||到OR ..仍然沒有。IF statement not working with OR

這裏是我的代碼仍然:

if($this->input->post('post_page') <> 10 
       || $this->input->post('post_page') <> 25 
       || $this->input->post('post_page') <> 50 
       || $this->input->post('post_page') <> 75) { 
      return false; 
     } 
+2

你想在這裏做什麼?這個if總是會返回false。 – looneydoodle

回答

1

我認爲你應該使用

if(!($this->input->post('post_page') == 10 
       || $this->input->post('post_page') == 25 
       || $this->input->post('post_page') == 50 
       || $this->input->post('post_page') == 75)) { 
      return false; 
} 
5

<>相當於不相等。

10不等於25因此它將進入if語句並返回false;

事實上,它總是會進入,如果聲明,無論數量

的你可以代替這樣做:

if($this->input->post('post_page') <> 10 
       && $this->input->post('post_page') <> 25 
       && $this->input->post('post_page') <> 50 
       && $this->input->post('post_page') <> 75) { 
      return false; 
     } 

在僞代碼:

IF 
MY NUMBER IS NOT 10 
AND IT IS NOT 25 
AND IT IS NOT 50 
AND IT IS NOT 75 
    RETURN FALSE 

甚至更​​好:

$allowedNumbers = array(10,25,50,75); 
if(!in_array($this->input->post('post_page'), $allowedNumbers)) { 
    return false; 
} 

也更容易將新項目添加到列表中。你添加到數組中的任何數字都不會返回false。

這一個僞代碼:

ALLOWED NUMBERS ARE 10,25,50,75 
IF(MYNUMBER IS NOT IN THE LIST OF ALLOWED NUMBERS) 
    RETURN FALSE 
+0

我很困惑...我想我總是會在IF語句中感到困惑,但是IF 10不等於10,然後返回false ..但它確實...我必須做&&而不是||? – Peanut

+0

我想做的是說,如果他們輸入的內容不等於這些數字中的任何一個,則返回false,因爲它必須是這4個數字中的任意一個。 – Peanut

+0

@Peanut,謝謝澄清,我更新了答案,實際上你可以交換||爲&& – cowls

0

翻譯布爾: 或真或假或真或真。 該表達式爲真,因此它返回false。

0
(       # 
    (A is not B)   # IS ALWAYS TRUE 
    OR (A is not C)   # -------------------------- 
    OR (A is not D)   # Unless 2 conditions (AND): 
    OR (A is not E)   # . A is not B 
)       # . B = C = D = E 

所以,是的,你的情況,這將永遠是假的,對不起你。