2011-01-28 189 views
1

嘿,所有。 我有一個processForm函數和一個displayForm函數。如果缺少表單字段,processForm函數將返回缺少字段的數組。這是所有罰款和丹迪直到我試圖將此數組包括到displayForm函數。 這裏的問題:PHP函數參數默認值

如果我不這樣做:

displayForm($missingFields=array()); 

然後我validateField函數拋出一個警告,它期待的參數是一個數組。 但是,會覆蓋由processForm函數返回的數組。

我希望我很清楚。謝謝你的幫助。

全碼:

if(isset($_POST['action']) && $_POST['action'] = "login") 
{ 
    $messages = processForm(); 
} 

processForm()

if($errorMessages) 
{ 
    return array("errors" => $errorMessages, "missing" => $missingFields); 
} 
else 
{ 
    $_SESSION['user'] = $user; 
    header("Location: index.php"); 
} 

form.php的

(!isLoggedIn())? displayForm($messages['errors']=array(),$messages['missing']=array()) : null; 

這些都是我在遇到麻煩的代碼段用。

再次感謝。

+0

請告訴我們你是如何使用該代碼,目前的問題*不是很清楚。 – deceze 2011-01-28 05:06:22

+0

好的,我會編輯上面的...幾分鐘... – Tableking 2011-01-28 05:07:15

回答

3

你不會在通話設置默認參數,你將它們設置在簽名,例如

function displayForm($arg1 = array()) { 
    ... 
} 

當你寫

displayForm($信息[」錯誤'] =數組())

這實際上是做這樣的事情

$messages['error'] = array(); // set $messages['error'] to an empty array 
displayForm($messages['error']); // pass an empty array to displayForm 

這是因爲在PHP中,賦值的返回值是分配的值。

0

你爲什麼要使用這樣的:

displayForm($messages['errors']=array(),$messages['missing']=array()) 

當你寫「$消息[ '錯誤'] =陣列()」,這是設置$內留言空白陣列。所以參數是空白的。你可以只寫:

displayForm($messages['errors'],$messages['missing'])