foreach()错误警告:为foreach()提供的无效参数

我不断收到一个错误数组,我在一个方法中设置了这个错误

这里是代码

public function showerrors() {
        echo "<h3> ERRORS!!</h3>";
        foreach ($this->errors as $key => $value)
        {
            echo $value;
        }
    }

我一直得到这个“警告:为foreach()提供的无效参数”当我运行程序,我在这样的构造函数中设置错误数组

 $this->errors = array();

所以我不完全确定它为什么不会打印错误!

public function validdata() {
        if (!isset($this->email)) {
            $this->errors[] = "email address is empty and is a required field";
        }

        if ($this->password1 !== $this->password2) {
            $this->errors[] = "passwords are not equal ";
        }
        if (!isset($this->password1) || !isset($this->password2)) {
            $this->errors[] = "password fields cannot be empty ";
        }
        if (!isset($this->firstname)) {
            $this->errors[] = "firstname field is empty and is a required field";
        }
        if (!isset($this->secondname)) {
            $this->errors[] = "second name field is empty and is a required field";
        }

        if (!isset($this->city)) {
            $this->errors[] = "city field is empty and is a required field";
        }


        return count($this->errors) ? 0 : 1;
    }

这里是我如何添加数据到数组本身! 感谢您的帮助!

好的,我把这个添加到了方法中

public function showerrors() {
        echo "<h3> ERRORS!!</h3>";
        echo "<p>" . var_dump($this->errors) . "</p>";
        foreach ($this->errors as $key => $value)
        {
            echo $value;
        }

那么它会在我的页面上输出这个

错误! 字符串(20)“无效提交!!” 如果我没有输入任何东西到我的文本框,所以它说一个字符串?

这里是我的构造函数,soory关于这个即时通讯新的PHP!

  public function __construct() {

        $this->submit  = isset($_GET['submit'])? 1 : 0;
        $this->errors = array();
        $this->firstname = $this->filter($_GET['firstname']);
        $this->secondname = $this->filter($_GET['surname']);
        $this->email = $this->filter($_GET['email']);
        $this->password1 = $this->filter($_GET['password']);
        $this->password2 = $this->filter($_GET['renter']);
        $this->address1 = $this->filter($_GET['address1']);
        $this->address2 = $this->filter($_GET['address2']);

        $this->city = $this->filter($_GET['city']);
        $this->country = $this->filter($_GET['country']);
        $this->postcode = $this->filter($_GET['postcode']);


        $this->token = $_GET['token'];
    }

在默认(没有填写)验证的情况下,您的表单中设置了"invalid submission"消息,您省略括号[] ,导致$this->errors被一个普通字符串覆盖,而不是附加到数组。

// Change
$this->errors = "invalid submission";

//...to...
$this->errors[] = "invalid submission";
链接地址: http://www.djcxy.com/p/53057.html

上一篇: foreach() error Warning: Invalid argument supplied for foreach()

下一篇: A local variable named 'port' cannot be declared