我在PHP中使用for循环,但是没有按预期工作。循环使用自己的验证脚本将表单输出到页面。但是,一旦运行,循环永远不会结束!
for ($noemployments=0; $noemployments < 3; $noemployments++) {
if ($noemployments >=9) {
break 2;
}
$noemploymentsErr.$noemployments="";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($_POST['no_empreturns".$noemployments"'])) {
$noemploymentsErr.$noemployments = "This is a required field.";
} else {
$no_empreturns.$noemployments = test_input($_POST['no_empreturns".$noemployments"']);
if (!preg_match("/^[a-zA-Z0-9_-]*$/", $no_empreturns.$noemployments)) {
$noemploymentsErr.$noemployments = "Illegal characters have been entered.";
}
}
}
print "<label for='no_returns'>Employment Name".$noemployments.":</label>
<input type='text' id='no_returns' class='form-control' name='no_empreturns".$noemployments."' placeholder='Employment name' maxlength='20'>";
print "<strong>".$noemploymentsErr.$noemployments."</strong>";
}
有什么建议吗?
答案 0 :(得分:1)
$noemploymentsErr.$noemployments="";
不做你的想法。它每次通过循环设置$noemployments
并清空字符串,其值为0
和0 < 3
。所以循环永远不会结束。
最好使用数组:
$noemploymentsErr[$noemployments] = "";
但如果您以后不再使用此功能,则无需单独保留错误,只需:
$noemploymentsErr = "";