为什么这是不可能做到的呢?
foreach($arr as $k => $v)
{
if($condition) { $obj->myMethod() && continue; }
}在计算$obj->myMethod()之后,将对关键字continue进行计算(执行),从而跳过当前的迭代。
编辑:我这样问是因为:
if($error) { $log->fatal('Something weird happened.') && continue; }是单行和不言自明。
发布于 2011-11-28 02:50:45
continue是一种语句,而不是表达式。
这两个人永远不会相遇。
不能在表达式中添加语句。( echo false && continue;会打印什么?)
相反,使用if,它可以包含语句。
发布于 2011-11-28 02:58:55
不能将“继续”计算为条件。other关键字的工作方式与其他语言不同。在PHP中,取决于上下文继续和中断可能在某种程度上是同义词,请考虑以下构造:
<?php
switch ($months)
{
// start with vowels
case 'august':
break;
case 'april':
continue; // exactly the same as "break" !!!
default:
return 'OK';
}
throw new StartsWithVowelException('Months with vowels are creepy');
?>在我们讨论这个主题时,break和continue关键字在PHP中有一个特性,这使得它们比其他语言中的同行更有趣和更强大。
当在循环中使用这两个参数时,都可以给出一个数值参数,该参数指示要继续执行或中断多少个循环。例如,下面是一个从内部循环中重新启动外部循环执行的示例:
<?php
//
// verify that each sub array contains the given value
//
$lowerval = strtolower($value);
foreach ($TwoDArray as $otherArray)
{
foreach ($otherArray as $value)
{
if (strtolower($value) == $lowerval)
{
// we found the value -- this one definitely has it.
continue 2;
}
}
// if we've reached here, then the inner loop doesn't have the
// value. ¡aiiee!
}
?>希望这能帮你解决这两个问题,祝你好运。
发布于 2011-11-28 03:04:46
continue是一种语句。在PHP中,语句和表达式是两种不同的东西,不能计算语句,因为它们本质上不返回true或false,这是PHP中计算的必要条件。
在PHP中,您必须执行如下操作:
if(test()) continue;https://stackoverflow.com/questions/8290984
复制相似问题