Php 是否可以返回“;继续&引用;在另一个公共方法内的for循环中?
我正试图精简我的长方法。我在Php 是否可以返回“;继续&引用;在另一个公共方法内的for循环中?,php,Php,我正试图精简我的长方法。我在forearch中有一个if,我想用一个方法替换它,类似于: /** * * @return skips majority of equities to reduce computations and tests limited equities */ public static function onlyTestLimitedEquities($equity_symbol) { if ($_SERVER['LOGNAME'] === ConstEQ::
forearch
中有一个if
,我想用一个方法替换它,类似于:
/**
*
* @return skips majority of equities to reduce computations and tests limited equities
*/
public static function onlyTestLimitedEquities($equity_symbol)
{
if ($_SERVER['LOGNAME'] === ConstEQ::LOCALHOST) {
if (HelperEQ::getOurSymbols($equity_symbol) == 0) {
continue;
}
}
}
if
语句在foreach中运行良好:
if ($_SERVER['LOGNAME'] === ConstEQ::LOCALHOST) {
if (HelperEQ::getOurSymbols($equity_symbol) == 0) {
continue;
}
}
如何返回继续代码>自EQ::onlyTestLimitedEquities($equity\u symbol)
将不在foreach中,或者如何创建/调用此方法
环
在另一种方法中,我有一个foreach,其中可以调用EQ::onlyTestLimitedEquities
:
foreach ($large_all_data_object->large_all_symbols_array as $equity_symbol => $equity_symbol_array) {
EQ::onlyTestLimitedEquities($equity_symbol);
}
您需要将继续
放在循环所在的位置。不能将其放置在其他函数中
但是,您可以将函数更改为返回布尔值,然后在主代码中执行continue
,条件是函数返回true
:
public static function onlyTestLimitedEquities($equity_symbol)
{
return $_SERVER['LOGNAME'] === ConstEQ::LOCALHOST
&& HelperEQ::getOurSymbols($equity_symbol) == 0;
}
。。。在调用代码中:
foreach ($large_all_data_object->large_all_symbols_array as $equity_symbol => $equity_symbol_array) {
// ...
if (EQ::onlyTestLimitedEquities($equity_symbol)) continue;
// ...etc
}
考虑到您的代码已经正常运行并且顺序相当好,您的问题可能更适合我们的姐妹站点。如果您不这样做,continue
必须在它控制的循环中出现。