停止 if 子句以及整个 PHP 脚本,但不停止 HTML

发布于 2024-10-18 04:49:46 字数 525 浏览 4 评论 0原文

我已经被这个问题困扰了大约一个小时,并在互联网上搜索答案。我检查了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷…雨湿花 2024-10-25 04:49:46

只需将脚本的其余部分包装在一个新的 if 块中:

$execute = true;

// HTML here...
if (empty($_POST['exampleEmail'])) {
    echo "Please enter an e-mail.";
    $execute = false;
}
// HTML here...
if ($execute) {
    // do stuff only if execute is still true.
}
// HTML here...

既然这对您有用,我建议您进行一些研究以分离您的表示和逻辑。有很多关于这个主题的教程和博客,您只需要开始搜索即可。

Just wrap the rest of the script in a new if block:

$execute = true;

// HTML here...
if (empty($_POST['exampleEmail'])) {
    echo "Please enter an e-mail.";
    $execute = false;
}
// HTML here...
if ($execute) {
    // do stuff only if execute is still true.
}
// HTML here...

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.

绝情姑娘 2024-10-25 04:49:46

您应该在 PHP 中使用 if 语句

if ($_POST['exampleEmail'] == "")
{
    echo "Please enter an e-mail.";
    $stop_script = true; //stop the script
}
else    $stop_script = false; //or continue the script

if($stop_script === false)
{
    //php scripts
}

//html

You should use an if-statement in PHP

if ($_POST['exampleEmail'] == "")
{
    echo "Please enter an e-mail.";
    $stop_script = true; //stop the script
}
else    $stop_script = false; //or continue the script

if($stop_script === false)
{
    //php scripts
}

//html
岁月染过的梦 2024-10-25 04:49:46

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文