2016-07-25 33 views
1

有關此問題的上下文,請參閱我的recent post。在下面的函數中,break continue將允許我省略最後的if條件。 (見在內部for循環的註釋)PHP中存在「break continue」嗎?

function strposHypothetical($haystack, $needle) { 

    $haystackLength = strlen($haystack); 
    $needleLength = strlen($needle);//for this question let's assume > 0 

    $pos = false; 

    for($i = 0; $i < $haystackLength; $i++) { 
     for($j = 0; $j < $needleLength; $j++) { 
      $thisSum = $i + $j; 
      if (($thisSum > $haystackLength) || 
       ($needle[$j] !== $haystack[$thisSum])) 
        break; 
      // if I could "break continue" I could omit the 
      // if ($j === $needleLength) 
      // below and just write $pos = $i; break;  
     } 
     if ($j === $needleLength) { 
      $pos = $i; 
      break; 
     } 
    } 
    return $pos; 
} 

我見過有些類似的帖子,如this一個不太回答我的問題。我不想重構上面的函數。我不需要break 2也不需要continue 2。注意break 2在內循環中不起作用,因爲外循環中的必要迭代將被忽略。 continue 2也失敗,因爲內循環需要迭代到針的末端。在PHP中可以使用break continue嗎?

我已經試過這一點,得到了一個致命錯誤,所以我認爲無論答案是「沒有」,我實現它不正確。

注2通過 「中斷繼續」 我的意思打破則內循環繼續外

+2

你期望'break continue'實際上能做什麼?它有點矛盾 –

+0

打破內在和外面的下一個迭代,繼續跳過$ POS並在它下面突破,這是我想要做的 –

+0

@self你可以展示更多這個,它不是在PHP手冊 – Martin

回答

3

有繼續(跳過)

<?php 
    while (list($key, $value) = each($arr)) { 
    if (!($key % 2)) { // skip even members 
     continue; 
    } 
    do_something_odd($value); 
} 

?> 

http://php.net/manual/en/control-structures.continue.php

for($i = 0; $i < $haystackLength; $i++) { 
    for($j = 0; $j < $needleLength; $j++) { 
     $thisSum = $i + $j; 
     if (($thisSum > $haystackLength) || 
      ($needle[$j] !== $haystack[$thisSum])) 
      continue; 
    } 
    if ($j === $needleLength) { 
     $pos = $i; 
     break; 
    } 
} 
+0

@Martin謝謝回答更正 – scaisEdge

+0

@馬丁..不由我..我不能刪除評論.. – scaisEdge

+0

哈哈,不,我的意思是我的評論被刪除,因爲我提出的問題不再存在於答案。在這是一件好事':-)' – Martin

1

不幸的是,在PHP中沒有像「break continue」這樣的構造,然而,通過將所有邏輯移動到內部循環中,您可以避免出現標誌並且完全需要「中斷繼續」。根本沒有真正簡化邏輯,思想,所以YMMV。

function strposHypothetical($haystack, $needle){ 
    $haystackLength = strlen($haystack); 
    $needleLength = strlen($needle); 

    $pos = false; 

    for($i = 0; $i < $haystackLength; $i++){ 
     for($j = 0; $j < $needleLength; $j++){ 
      $thisSum = $i + $j; 
      if(($thisSum > $haystackLength) 
       || ($needle[ $j ] !== $haystack[ $thisSum ])) 
       break; 
      else if($j === $needleLength){ 
       $pos = $i; 
       break 2; 
      } 
     } 
    } 

    return $pos; 
} 

爲了解決性能,你甚至可以這樣重構來,加上由不存儲在變量$ I + $ j的結果有點表現,但確實擺脫額外的變量,並使得$ POS標誌樣:

function strposHypothetical($haystack, $needle){ 
    $haystackLength = strlen($haystack); 
    $needleLength = strlen($needle); 

    $pos = false; 

    for($i = 0; $i < $haystackLength; $i++) { 
     $pos = $i; 
     for($j = 0; $j < $needleLength; $j++) { 
      if(
       ($pos + $j > $haystackLength) 
       || ($needle[ $j ] !== $haystack[ $pos + $j ]) 
      ){ 
       $pos = false; 
       break; 
      } 
     } 

     if($pos !== false) 
      break; 
    } 

    return $pos; 
} 

得到了一些時間昨晚去思考,這應該重構通過強制的比較得到最大的性能和分配for循環已經執行完成大部分工作。它也縮短了代碼;

function strposHypothetical($haystack, $needle){ 
    $haystackLength = strlen($haystack); 
    $needleLength = strlen($needle); 

    for($i = $haystackLength, $pos = false; $i >= 1 && $pos === false; $i--){ 
     for($j = $needleLength - 1, $pos = $i - 1; $j >= 0 && $pos !== false; $j--){ 
      $pos--; 
      if($pos < 0 || $needle[ $j ] !== $haystack[ $pos ]) 
       $pos = false; 
     } 
    } 

    return $pos; 
} 
+1

謝謝你的迴應,但是恭敬地說,這實際上更糟,因爲現在if($ j === $ needleLength)比較沒有完成$ haystackLength時間(max),而是$ haystackLength * $ needleLength時間(最大值)。 –

+1

我知道。這就是爲什麼我把YMMV。但是,它確實會從代碼中獲得中斷的概念。如果沒有完全重構就可以刪除這個概念,並且你說你不需要這些代碼,那麼你可以用代碼做的事情不多。認爲一些代碼比僅僅說沒有休息繼續或給予討厭的重構去除所有代碼並且只是放入'返回strpos($ haystack,$ needle)好;' –

+1

OK ...讚賞。我確實瞭解SO上的流行反應是如何說「你的代碼不好 - 你應該做的是這個......」。對您的評論和回覆進行投票(因爲您的代碼確實有效)。 –

相關問題