4

我是编码新手,无法为简单的逻辑建立想法。
好吧,我已经写了一个代码,首先看看:

<?php
    function month_ana($mna){
        $c_mna = strlen($mna);
        echo "Let's Analysis <b>$mna</b> <br> Total Charectars: $c_mna <br>";
    }
    $go[0] = "January";
    $go[1] = "February";
    $go[2] = "March";
    $go[3] = "April";
    $go[4] = "May";
    $go[5] = "June";
    $go[6] = "July";
    $go[7] = "August";
    $go[8] = "September";
    $go[9] = "October";
    $go[10] = "November";
    $go[11] = "December";
    $fo = "October";
    $i = 0;
    for($i=0;$i<=11;$i++){
        if ($go[$i]==$fo){
            break;
        } else { 
            month_ana($go[$i]);
        }
    }
?>

我想做的是删除匹配的变量,所以函数会跳过它。
让我们看看它的输出:

Let's Analysis January 
Total Charectars: 7 
Let's Analysis February 
Total Charectars: 8 
Let's Analysis March 
Total Charectars: 5 
Let's Analysis April 
Total Charectars: 5 
Let's Analysis May 
Total Charectars: 3 
Let's Analysis June 
Total Charectars: 4 
Let's Analysis July 
Total Charectars: 4 
Let's Analysis August 
Total Charectars: 6 
Let's Analysis September 
Total Charectars: 9 

但问题是我使用break();了语句,所以当变量匹配时它停止工作October,但我希望这样,这将跳过匹配的变量而不是在这里停止,所以在这种情况下它应该跳过October然后重新开始计算November并继续。我希望你明白这个问题。有任何想法吗?

如果您认为应该向我推荐一些文章,那么请这样做,因为我非常热衷于学习 PHP 编程。谢谢

4

3 回答 3

2

尝试continue;

var $go = array('January', 'February', 'March', ...);

foreach ($go as $month) {
   if ($month == $fo){
      continue;
   } 
   echo $month . '<br />';
}
于 2012-12-31T12:51:50.230 回答
1

使用关键字continue而不是break

于 2012-12-31T12:50:54.403 回答
0
for($i=0;$i<=11;$i++)
{
  if ($go[$i] != $fo)
  {
    month_ana($go[$i]);
  }
}

我放了'!='而不是'=='。

如果你使用continue它会再次开始循环,但你说你想在分析完 10 月后继续 11 月。

于 2012-12-31T12:59:03.803 回答