如果使用 PHP,则跳过父级

发布于 2024-11-30 09:13:18 字数 399 浏览 2 评论 0原文

不确定这是否可能,但代码的基本思想如下:

if(isset($_POST['submit'])) {
    if($_POST['field'] == 0) {
        // do not process the code in the if statement
    }
    // code to process if the above validation criteria is not met
}

我基本上想尝试并使其尽可能简单,而不是到处都有大量的 if,只是顶部的快速验证语句,并且如果没有那些 if 语句被触发,它将处理代码以更新数据库等。

我尝试了 continue 语句函数,但没有任何喜悦,我相信它只适用于循环。

谢谢!

Not sure if this is even possible, but the basic idea of the code is as follows:

if(isset($_POST['submit'])) {
    if($_POST['field'] == 0) {
        // do not process the code in the if statement
    }
    // code to process if the above validation criteria is not met
}

I basically want to try and keep it as simple as possible without lots of if's everywhere, just the quick validation statements at the top, and if none of those if statements are triggered, it will process the code to update the database etc.

I have tried the continue statement function with no joy, I believe that only works with loops.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

愚人国度 2024-12-07 09:13:18
if(isset($_POST['submit']) && $_POST['field'] != 0) {

}

作为一个函数

function testCondition() {
   return isset($_POST['submit']) && 
          $_POST['field'] != 0;
}

if (testCondition()) { ... }
if(isset($_POST['submit']) && $_POST['field'] != 0) {

}

As a function

function testCondition() {
   return isset($_POST['submit']) && 
          $_POST['field'] != 0;
}

if (testCondition()) { ... }
咆哮 2024-12-07 09:13:18

您可以将所有这些放入一个函数中,然后从“if”内部调用“return”:

if(isset($_POST['submit']) && validate_form())
{
    ...
}

function validate_form()
{
    if($_POST['field'] == 0) {
        return false;
    }
    if(another check that fails) {
        return false;
    }
    ...
    return true;
}

You could put all of this in a function and then call 'return' from the inside 'if':

if(isset($_POST['submit']) && validate_form())
{
    ...
}

function validate_form()
{
    if($_POST['field'] == 0) {
        return false;
    }
    if(another check that fails) {
        return false;
    }
    ...
    return true;
}
太阳哥哥 2024-12-07 09:13:18

要按照您最初的期望完成此操作,您需要使用 break 语句。 来自 PHP 手册

$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
while (list(, $val) = each($arr)) {
    if ($val == 'stop') {
        break;    /* You could also write 'break 1;' here. */
    }
    echo "$val<br />\n";
}

/* Using the optional argument. */

$i = 0;
while (++$i) {
    switch ($i) {
    case 5:
        echo "At 5<br />\n";
        break 1;  /* Exit only the switch. */
    case 10:
        echo "At 10; quitting<br />\n";
        break 2;  /* Exit the switch and the while. */
    default:
        break;
    }
}

To accomplish this as you originally desired, you need to use the break statement. From the PHP manual:

$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
while (list(, $val) = each($arr)) {
    if ($val == 'stop') {
        break;    /* You could also write 'break 1;' here. */
    }
    echo "$val<br />\n";
}

/* Using the optional argument. */

$i = 0;
while (++$i) {
    switch ($i) {
    case 5:
        echo "At 5<br />\n";
        break 1;  /* Exit only the switch. */
    case 10:
        echo "At 10; quitting<br />\n";
        break 2;  /* Exit the switch and the while. */
    default:
        break;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文