知道为什么不打印这些错误吗?
// Validate the email
if (preg_match($regex, $email))
$errors[] = "Invalid email address";
// ... and the password
if (strlen($password) < 4)
$errors[] = "Password is too short";
// No errors?
if (empty($errors))
{
// insert into db
}
// If there were any errors, show them
if (!empty($errors))
{
$errors = array();
foreach ($errors as $error)
echo '<li>'.$error.'</li>';
}
Run Code Online (Sandbox Code Playgroud)
你在输出之前覆盖了数组.
$errors = array(); // Creates an empty array
foreach ($errors as $error) // Won't do anything
Run Code Online (Sandbox Code Playgroud)
删除$errors = array()它应该工作.
$errors = array()在脚本的最开始初始化的方式会更清晰,然后检查count($errors) > 0而不是empty:
// No errors?
if (count($errors) == 0)
{
// insert into db
}
// If there were any errors, show them
else
{
$errors = array();
foreach ($errors as $error)
echo '<li>'.$error.'</li>';
}
Run Code Online (Sandbox Code Playgroud)
这样,您将避免关于$error未设置的通知.