PHP - 从函数返回数组

时间:2014-09-23 17:43:37

标签: php mysql sql post

我使用以下函数(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);
    }

这是验证表单(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



                }

上面的代码返回:

Notice: Undefined variable: error in /home/.../public_html/pages/prize.php on line 27

虽然它不应该在那里抛出错误,因为die($success)应该由代码100触发。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

$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);
            }

首先猜测if($validateForm['code'] == "100"){是在转换之外。

$validateForm = $validate->validateForm();

返回一个数组..稍后你在开关中做if ($validateForm==1) ..当$ validateForm是一个数组。

使用简单的is_array() if语句比使用整个开关

更好运