[英]PHP - Return array from function
I am submitting a form, using the following function (prize.php)
: 我正在使用以下功能
(prize.php)
提交表单:
loadmodule('validate'); //This just loads the validate.php function.
$validate = new Validate;
if($_POST)
{
$validateForm = $validate->validateForm();
switch($validateForm)
{
case 1:
$error = 'You\'re not logged in..';
$stop = true;
break;
//If no error = success.
if($validateForm['code'] == "100"){
$won = $val['prize'];
$type = $val['type'];
$success = 'You have won! The prize was '.$won.' '.$type.'';
die($success);
}
}
die($error);
}
This is the function to validate the form (validate.php)
: 这是验证表单
(validate.php)
:
function validate()
{
global $userdata;
if(!is_array($userdata))
return 1; // User not logged in - return error code one.
//If no error, lets show a success message.
$prize = "100";
$text = "dollars";
return array("code"=>"100","prize"=>"$prize","type"=>"$text");
}//If won
}
The above code returns: 上面的代码返回:
Notice: Undefined variable: error in /home/.../public_html/pages/prize.php on line 27
Although, it shouldn't throw an error there, since the die($success)
should be triggered by the code 100. 虽然,它不应在此处引发错误,因为
die($success)
应该由代码100触发。
What am I doing wrong? 我究竟做错了什么?
$error = '';
$stop = false;
switch($validateForm){
case 1:
$error = 'You\'re not logged in..';
$stop = true;
break;
}
//If no error = success.
if($validateForm['code'] == "100"){
$won = $val['prize'];
$type = $val['type'];
$success = 'You have won! The prize was '.$won.' '.$type.'';
die($success);
}
first guess is that the if($validateForm['code'] == "100"){
is ment to be outside the switch. 第一个猜测是
if($validateForm['code'] == "100"){
是在交换机外部。
$validateForm = $validate->validateForm();
returns an array.. later on your're doing a if ($validateForm==1)
in the switch.. when $validateForm is an array. 返回一个数组..稍后在$ validateForm是一个数组时在开关中执行
if ($validateForm==1)
。
you might have better luck with a simple is_array()
if statement than the whole switch 使用简单的
is_array()
if语句可能比整个开关要好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.