2016-05-17 51 views
-4

我的代碼:情況如果在PHP

function insertChamado($id, $area = 2) 
    { 

    if ($area != 2 && $area != 4) 
    $area = 2; 

如何調整這個代碼不接受0的條件爲顯示日誌波紋管在:

[12-May-2016 16:58:28 America/Sao_Paulo] id = 36445, area = 0 
[12-May-2016 16:59:00 America/Sao_Paulo] id = 14635, area = 0 
[12-May-2016 17:00:02 America/Sao_Paulo] id = 18599, area = 0 
+1

不接受?你想要它做什麼? 'if($ area == 0){//不接受}'。 –

+0

你的條件陳述永遠不會等於2「和」4,你想要「或」。 –

+1

@Fred你確定嗎?那麼area = 3呢? :) – splash58

回答

0

仔細閱讀您的問題後,我認爲您的最佳解決方案是一個簡單的switch

function insertChamado($id, $area = 2){ 
    switch ($area) { 
     case 2: 
      echo "area equals 2\n"; 
      break; 
     case 4: 
      echo "area equals 4\n"; 
      break; 
     default: 
      echo "area is always 2 other wise\n"; 
    } 
} 

insertChamado('id',0); // will output "area is always 2 other wise" 

insertChamado('id'); // will output "area equals 2" 
+0

我們經歷了所有這些,這個傢伙只是想知道如何使用開關盒?他可能只是使用if/else,如果這樣的話大聲笑.... – Wobbles

2

只需添加一個條件,以檢查對於它...不知道什麼是很難的,除非我們錯過了一些東西。

function insertChamado($id, $area = 2) 
    { 
     if ($area == 0) die("Ruh-Rohh"); 
     if ($area != 2 && $area != 4) 
      $area = 2; 
    } 

或者,如果你希望它是2,如果是0:

function insertChamado($id, $area = 2) 
    { 
     if (($area != 2 && $area != 4) || $area == 0) // Though || $area == 0 actually does nothing here as 0 already meets the previous condition. 
      $area = 2; 
    } 

它的事實後,會出​​現對我來說,$面積也從來沒有在原始代碼爲0是0! = 2和0!= 4因此$ area = 2。我懷疑是一個實現問題,如果這沒有幫助,我建議你編輯你的問題以包含更多的代碼。

可能是範圍問題,因爲您沒有使用全局$區域並且沒有返回值,所以更改後的$區域可能不會突破該函數。

嘗試本身的實現方式之一:

使用全局

$area = 0; // for testing only 
function insertChamado($id) 
{ 
    global $area; 
    if ($area != 2 && $area != 4) 
     $area = 2; 
} 

,或者使用回:

$area = insertChamado(0,0); 
function insertChamado($id, $area = 2) 
{ 
    if ($area != 2 && $area != 4) 
     $area = 2; 
    return $area; 
} 

您提供沒有幫助,因爲我不知道該殘缺碼是什麼執行id是。

+2

* Ruh-Rohh * - 那是一個史酷比「哦 - 哦」嗎?大聲笑 –

+0

@ Fred-ii-它是一個愚蠢的問題,所以我不得不添加一些幽默,以防止瘋狂大聲笑 – Wobbles

+0

嘿嘿,是的,史酷比的聲音! –

相關問題