我可以调用 continue 作为返回或调用它吗?

Posted

技术标签:

【中文标题】我可以调用 continue 作为返回或调用它吗?【英文标题】:Can I call continue as a return or invoke it? 【发布时间】:2015-09-08 23:29:18 【问题描述】:

在我的循环中,我有一个返回 true 或 false 的回调。 取决于结果,我必须继续或中断我的循环。

如果可能的话,现在我希望这一步更容易。 我将在我的回调中返回“继续”或“中断”,这样说的话会很好:

call($return) // $return => 'continue' or 'break'

这可能吗?

编辑 简而言之:

$return = $this->myCallback(...);
if ($return)  break; 
if (!$return)  continue; 

相反,我想要这样的东西:

$return = $this->myCallback(...);
call($return); // return contains 'continue' or 'break'

【问题讨论】:

看看:php.net/manual/en/control-structures.continue.phpphp.net/manual/en/control-structures.break.php 何不试一试? 不可能在回调中只说中断/继续。所以我的问题是我需要调用什么函数或调用。 为什么不 $continue = true or false? 也许,你显示代码片段? 【参考方案1】:

不...您可以从函数中返回布尔值并检查您是要中断还是继续..

【讨论】:

闻起来有待评论。 呃?是不是很简单?【参考方案2】:

而不是

$return = $this->myCallback(...);
if ($return)  break; 
if (!$return)  continue; 

if($this->myCallback()) break;
else continue;     // if yoг are really need this continue

为什么不呢?

【讨论】:

以上是关于我可以调用 continue 作为返回或调用它吗?的主要内容,如果未能解决你的问题,请参考以下文章

Spring Actuator Controller 端点在哪里,我可以使用 jvm 调用以编程方式调用它吗?

我可以在python中创建一个锁或信号量,而不必直接作为参数传递它吗?

我误解了 continue 语句吗?

jQuery ajax调用返回URL并加载到框架或div中

[转帖]Shell脚本中的break continue exit return

PHP中跳出循环break,continue,return,exit的区别