PHP-返回后中断? [英] PHP - Break after return?

查看:47
本文介绍了PHP-返回后中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在此处使用break还是停止循环并仅返回一次?

do I need to use break here or will it stop looping and just return once?

for($i = 0; $i < 5; $i ++) {
    if($var[$i] === '') return false;
    // break;
}

谢谢!

推荐答案

它将只运行一次,停止循环,然后退出函数/方法。

It will run just once, stop looping, and exit from the function/method.

尽管这样这是坏风格。很容易忽略以后再返回 ,这对于调试和维护很不利。

It could be argued though that this is bad style. It is very easy to overlook that return later, which is bad for debugging and maintenance.

使用 break 可能更干净:

for($i = 0; $i < 5; $i ++) {
    if($var[$i] === '')
     { set_some_condition; 
       break;
     }
}

if (some_condition)
 return;

这篇关于PHP-返回后中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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