【问题标题】:Does return ends function if ifelse is used? [closed]如果使用 ifelse,是否返回结束功能? [关闭]
【发布时间】:2023-03-09 02:10:01
【问题描述】:

我在 if else 语句中返回了一些东西。它是结束整个功能还是仅结束 ifelse 范围?

function user () {
    if (//first) {
        return TRUE;
    }
    //somecode

    if (//second) {
        return FALSE;
    }

    return TRUE;
}

在上面的代码中,如果我的第一个条件为真,它是结束整个函数还是只结束第一个 ifelse 范围?

【问题讨论】:

  • 你自己试试看吧?
  • 来自PHP Docs:If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call..... 在询问 StackOverflow 之前阅读 PHP 文档真的很有用,可以节省您和我们的时间
  • 在您发表评论后,我将自己尝试这些愚蠢的问题。谢谢你! :) 顺便说一句,我试过这个,但我更困惑。
  • @MarkBaker 我总是首先阅读 PHP 文档。但我就是无法摆脱这种困惑。
  • 我建议你阅读 return and continue 和 break and exit and die 这些基础知识

标签: php function if-statement scope return


【解决方案1】:

return 结束整个函数。 总是

因此,如果您的第一个条件是 true,则不会执行 user 函数中的其他操作,并且函数只会返回 true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2013-08-07
    相关资源
    最近更新 更多