<?php
class Validate
{
public $errors = array();
public function date($date)
{
if(!strtotime($date))
{
$errors['date'] = "$date is not a valid date.";
//var_dump($errors);
} else {
return $date;
}
}
public function get_errors()
{
foreach($this->errors as $error => $val)
{
return $val."<br />\n";
}
}
}
$validate = new Validate;
$validate->date('<20 Feb, 2013');
print $validate->get_errors();
它应该循环将$errors
它们打印到浏览器。它没有。我确定$errors
数组通过以下语句初始化:
var_dump($errors);
印刷:
array(1) { ["date"]=> string(34) "<20 Feb, 2013 is not a valid date." }
到屏幕。我错过了什么?
我尝试了各种形式的 foreach 构造:
foreach($this->errors as $this->error => $val)
{
return $val."<br />\n";
}
和
foreach($this->errors as $this->error => $this->val)
{
return $this->val."<br />\n";
}
但没有快乐!