2011-01-28 118 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'])