停止 if 子句以及整个 PHP 脚本,但不停止 HTML
我已经被这个问题困扰了大约一个小时,并在互联网上搜索答案。我检查了 PHP 文档,环顾四周,谷歌搜索,什么也没有。
无论如何,我的问题是,在我尝试验证某些内容(这是错误的)之后,如果我使用 exit; 它也会停止 HTML。这就是我要说的:
if ($_POST['exampleEmail'] == "")
{
echo "Please enter an e-mail."; //Now I want only the PHP script to stop, however...
exit; //If I use exit, then the HTML after this script (footer) doesn't show.
}
如果有人可以提供帮助,请帮忙。我尝试过使用break,但无济于事,因为它仅适用于循环和开关。
如果有更好/更正确的方法(或者如果这是错误的方法,则只是更正),请分享。我过去也遇到过这个问题,然后我就使用了 exit 。
谢谢。
I've been having this problem for around an hour, and have searched the internet for answers. I've checked the PHP documentation, looked around, Googled, nothing.
Anyways, my problem is that after I try to validate something (and it's wrong), if I use exit; it will also stop the HTML after. Here's what I'm talking about:
if ($_POST['exampleEmail'] == "")
{
echo "Please enter an e-mail."; //Now I want only the PHP script to stop, however...
exit; //If I use exit, then the HTML after this script (footer) doesn't show.
}
If anyone can help, please do. I've tried using break, but to no avail, since it's only for loops and switches.
If there is a better/more correct (or simply correct if this is the wrong way), please share. I've had this problem in the past, and I just used exit then.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需将脚本的其余部分包装在一个新的
if
块中:既然这对您有用,我建议您进行一些研究以分离您的表示和逻辑。有很多关于这个主题的教程和博客,您只需要开始搜索即可。
Just wrap the rest of the script in a new
if
block:Now that this is working for you, I advise you to do some research into separating your presentation and your logic. There's a lot of tutorials and blogs on the subject, you just need to start searching.
您应该在 PHP 中使用 if 语句
You should use an if-statement in PHP
使用
break
http://php.net/manual/ en/control-structs.break.php 代替。但如果首先涉及到这个用例,您可能会遇到结构性问题。Use
break
http://php.net/manual/en/control-structures.break.php instead. But you probably have a structural Problem if it comes to this usecase in the first place.