PHP-停止执行调用函数 [英] PHP - Stopping the execution of the calling function

查看:59
本文介绍了PHP-停止执行调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点难以解释,但我会尽力而为。

This is a little difficult to explain, but i'll try my best.

这类似于我当前正在运行的代码。

This is similar to the code i'm currently running.

function func1()
{

     func2()

}

function func2()
{

     exit();

}

我需要能够从以下位置停止执行func1在func2中,但是我需要比exit()更好的方法。由于func1的调用者可能需要执行其他操作,因此我也不想停止它们,我只想停止执行func1的其余部分。

I need to be able to stop the execution of func1 from within func2, but I need a better way to do it than exit(). Since the caller of func1 might have other operations to do, I don't want to stop them as well, I just want to stop the rest of func1 from executing.

我知道我可以在func1中放置一个简单的if语句来检测func2的返回值,然后在确定值后返回,但这也有些混乱。

I'm aware that I could put a simple if statement in func1 to detect the return value of func2 and then return if it's a certain value, but that's a bit messy too.

所以我的问题是,如何才能从函数内部停止执行调用方函数的其余部分?

So my question is, how can I stop the execution of the remainder of the caller function, from within a function?

推荐答案

返回值并进行检查。

function func1()
{
    $result = func2();
    if ($result === 0) return;

    // Your func1 code here
}

function func2()
{
    if ( ... ) return 1;
    else return 0;
}

这篇关于PHP-停止执行调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆