2015-10-23 51 views
1

我正在創建函數,我希望能夠輸出與函數內的語句是否符合的真或假有關的函數。這方面的一個例子是這樣的:在函數中返回true/false以進一步解釋爲什麼返回false的原因

function pokeme($number){ 
    if($number > 10) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

然而,一個問題,這個方法是,如果函數返回false,您將無法破譯其中虛假意味着尤其是如果有什麼,不止一個,如果/ else語句。

我的問題是,我該如何輸出一個false加上一種方式來稍後識別那個錯誤是什麼?

我可以做一個數組嗎?

return array("false", "message pertaining to whatever"); 

然而,如果做到這一點,你不能真正做到這一點,再加上...:

if(pokeme()){ /*success*/ } else { /*there may be multiple falses for different situations... how do I distinguish what it is? */} 
+0

你應該檢查在PHP中拋出和處理異常。 – sotoz

+0

您可以定義一組規則,如*「field X是一個數字,它應該> Y,如果不是,則錯誤消息應該是Z」*。您可以在對象數組中定義這些規則並將其應用於表單輸入。您也可以下載[表單驗證器](http://www.html-form-guide.com/php-form/php-form-validation.html),而不是實現這樣的事情。 – GolezTrol

+0

正如sotoz所說,看看[例外](http://php.net/manual/en/language.exceptions.php)。這是你想要的嗎? – FirstOne

回答

2

請注意,這裏展示的想法可能並不是最好的,但一旦你掌握了它,它會變得更容易。另請閱讀結束註釋。

如果你想使用這樣的(是真的期望和假的問題):

if(pokeme()){ /*success*/ } else { /* not needed */} 

你可以做這樣的事情:

function pokeme($number){ 
    //let's say you want to return true if it's >10 and -9 to -1 
    if($number > 10){ 
     // do something 
     return true; 
    } 
    if($number < 0 && $number > -10){ 
     return true; 
    } 
    // handling multiple problems (just 2 ^^) 
    if($number < -9){ 
     throw new Exception("Invalid input. Can't use negative smaller than -9."); 
    } 

    throw new Exception('Invalid input. Expected bigger than 10.'); 
} 

兩個測試:

try{ 
    echo "RESULT1 :".pokeme(-42).":"; // not shown (error in this line) 
    echo "RESULT2 :".pokeme(11).":"; // not shown 
}catch(Exception $e){ 
    echo "Error: '".$e->getMessage()."'"; // Just the message 
} 

echo "<br><br>"; 

try{ 
    echo "RESULT3 :".pokeme(11).":<br>"; // shown 
    echo "RESULT4 :".pokeme(10).":"; // not shown (error in this line) 
}catch(Exception $e){ 
    echo $e; // Full error 
} 

你可以這樣使用它:

try{ 
    if(pokeme(11)){ 
     echo "VALID INPUT<br>"; 
    } 
    if(pokeme(5)){ 
     echo "I'm not seen :\\"; 
    } 
}catch(Exception $e){ 
    echo "Error: '".$e->getMessage()."'"; 
} 

結束註釋:想象一下,您正在使用可能會導致錯誤的內置php函數。這樣你必須用try..catch來處理它。

更多關於Exceptions

1

對於全自動輸入驗證你可以使用Symfony的形式組成的,並其Validation。 您還可以添加非常簡單的約束LessThan(10)一樣,在您的示例中,組件會自動將適當的錯誤消息寫回到您的頁面(以及無效的表單,例如不執行數據庫插入)。 它存在很多準備使用的約束條件,您也可以創建自己的約束條件。

或者如果你想自己寫所有,我建議你閱讀之前OOPException handling

編輯

如果你想「收」的錯誤與它的消息,解決這個程序爲您的代碼示例(不推薦),你可以在超全局變量$ _SESSION臨時數組存儲此消息。我明確地說超全球變量。不要使用全局變量,並用global鍵注入它,這將成爲long therm中非常複雜的代碼。

無論如何,我的想法使用$ _SESSION。此代碼的工作對我來說:

<?php 
session_start(); 

// Resetting the tmp session array on every page reload. That previous error messages are resetted. 
$_SESSION['errors'] = array(); 

function printSessionErrors() { 
    // Print errors stored in the session if some exists 
    if(array_key_exists('errors', $_SESSION)) { 
     foreach($_SESSION['errors'] as $i => $error) { 
      $success = true === $error['success'] ? 'true' : 'false'; // Only for print 
      $message = $error['message']; 
      echo "Success: $success. Message: $message <br>"; 
     } 
    } 
} 

function pokeme($number){ 
    $expected = 10; 
    $success = null; 

    if($number > $expected) 
    { 
     $success = true; 
    } 
    else 
    { 
     $_SESSION['errors'][] = array('success' => $success, 
             'message' => "Number $number is less than $expected"); 
     $success = false; 
    } 

    return $success; 
} 

pokeme(1); 
pokeme(7); 
pokeme(99); 
printSessionErrors(); 

現在依靠,如果它是一個表單POST或程序驗證您添加在上面printCleanSessionErrors()session_start()後)或上的代碼底部。

我得到這樣的輸出:

Success: false. Message: Number 1 is less than 10 
Success: false. Message: Number 7 is less than 10 

你只需要在$_SESSION['errors'][] = array ....添加到您的其他錯誤情況。

+0

剛剛更新了我的問題。 – Majo0od

+0

@ Majo0od我現在編輯了我的答案,因爲您的更新而提供給您一個具體的解決方案。 –