使用 array_push() 帮助
我有一个包含错误数组的变量,
$errors = array();
我还有一个 if 语句,用于返回用户名是否已在输入中输入。
if(isset($_POST['submit'])) {
if(empty($_POST['username'])) {
echo array_push($errors, 'You did not submit a username' );
}
}
我使用 array_push() 在其末尾添加错误消息。我使用 for every 循环来检索所有错误字段的值。虽然我不断获取数组值的数量以及预期的字符串......例如,它会回显“1您没有提交用户名”
foreach($errors as $e) {
echo $e;
echo "<br />\n";
}
是否有只检索所需的字符串?
I have a variable holding an array of errors
$errors = array();
I also have an if statement that returns whether or not a username has been entered in an input.
if(isset($_POST['submit'])) {
if(empty($_POST['username'])) {
echo array_push($errors, 'You did not submit a username' );
}
}
I'm using array_push() to add an error message at the end of it. I'm using a for each loop to retrieve the values of all the error fields. although I keep getting the number of array values as well as just the intended string.... For instance it will echo out "1 You did not submit a username"
foreach($errors as $e) {
echo $e;
echo "<br />\n";
}
is there anyway to retrieve just the required string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有一个额外的回声:
You have an extra echo:
从
echo array_push($errors, 'You did not Submit a username' );
中删除echo
。这是不需要的,这就是与结果中的 1 相呼应的内容。Remove
echo
fromecho array_push($errors, 'You did not submit a username' );
. It's not needed, and that is what's echoing the 1 in your result.