2012-02-21 182 views
0

我遇到的問題是PHP不能正確處理我的continue。我的代碼如下:PHP`繼續`不按預期工作

$count = -1; 
foreach ($lines as $item){ 
    $count++; 

    if ($item == ""){ 
     continue; // don't process empty items 
    } 
    if ($count > 0){ 
     echo '-not the first line-'; 
    } 

    switch ($item){ 
     case "item1": 
      echo "item 1 found"; 
     break; 
    } 
} 

if ($count > 0)部分執行無論在哪裏continue放在之前,但switch按預期執行。

如果我刪除if ($count > 0),並把它開關,它按預期執行:

switch ($item){ 
     case "item1": 
      if ($count > 0){ 
       echo '-not the first line-'; // executes as expected 
      } 
      echo "item 1 found"; 
     break; 
    } 

這是正常的嗎?謝謝

+0

你能提供'$ lines'的內容是什麼? – Vitamin 2012-02-21 10:18:45

+0

繼續在這種情況下只會執行,如果你的$ item是「」,是這樣嗎? – linuxeasy 2012-02-21 10:19:20

+2

適合我:http://codepad.viper-7.com/cKTFYH – PeeHaa 2012-02-21 10:20:28

回答

2

如果($ count> 0)部分執行,而不管繼續放在它之前的位置,但交換機按預期執行。

您的期望是它不執行?您將$ count設置爲「-1」,然後用$ count ++增加它。在我的書中,$ count將是0,它不會大於0,這意味着if條件是真的嗎?

編輯:基於我從評論中得到的信息,您沒有使用第一行的內容,而您僅使用$ count來跟蹤迭代次數?如果是這樣;出現以下情況:

<?php 

$lines = array(
    'first', 
    'foo', 
    'bar', 
    'baz', 
    'item1' 
); 

$first = array_shift($lines); // remove it if you don't use it? 
$lines = array_filter($lines); // remove empty lines. 
$count = 0; // set count to 0, although you could just count($lines) as well. 

foreach ($lines as $item){ 
    $count ++; 
    echo '-not the first line-'."\n"; 

    if($item === 'item1') { 
     echo "item 1 found\n"; 
    } 
} 
+0

@BarryLangerak:是的,但它需要檢查字符串是否爲空。如果字符串爲空,則繼續並忽略循環的其餘部分。除了'count> 0'部分之外,所有其餘的循環都被忽略。 'count> 0'被用來檢查它是否是第一行,並在代碼 – aggregate1166877 2012-02-21 10:35:31

+0

啊中跟蹤迭代次數。這有點解釋。你有沒有聽說過'array_shift()'函數? – 2012-02-21 10:50:14

+0

居然沒有,還沒有聽說過'array_shift()';謝謝 – aggregate1166877 2012-02-21 12:42:46

3

即使您沒有繼續阻止,您的第二個示例仍然適用於您。

$count = -1; 
foreach ($lines as $item){ 
    $count++; 

    switch ($item){ 
     case "item1": 
      if ($count > 0){ 
       echo '-not the first line-'; // executes as expected 
      } 
      echo "item 1 found"; 
     break; 
    } 
} 

如果您$item == ""這顯然是$item != "item1"所以唯一的情況下也不會被執行。在你的示例代碼中,這相當於之前有一個繼續。我的猜測是你的$ item不是你想象的那樣。試試var_dump($item)吧。

EDIT1:本地測試

<pre><?php 

$lines = array("", "", "John Doe", "", "2011", "", "", "item1"); 

$count = -1; 
foreach ($lines as $item){ 
    $count++; 

    if ($item == ""){ 
     echo "\t(skipping empty item)\n"; 
     continue; // don't process empty items 
    } 
    if ($count > 0){ 
     echo "-not the first line-\n"; 
    } 
    else 
     echo $item." is the first line\n"; 

    switch ($item){ 
     case "item1": 
      echo "item 1 found\n"; 
     break; 
    } 
} 
?> 

將輸出

(skipping empty item) 
    (skipping empty item) 
-not the first line- 
    (skipping empty item) 
-not the first line- 
    (skipping empty item) 
    (skipping empty item) 
-not the first line- 
item 1 found 

具有

$lines = array("foobar", "", "John Doe", "", "2011", "", "", "item1"); 

將輸出

foobar is the first line 
    (skipping empty item) 
-not the first line- 
    (skipping empty item) 
-not the first line- 
    (skipping empty item) 
    (skipping empty item) 
-not the first line- 
item 1 found 

在continue-statement下面移動$count++;,使其工作。

<pre><?php 

$lines = array("", "", "John Doe", "", "2011", "", "", "item1"); 

$count = -1; 
foreach ($lines as $item){ 

    if ($item == ""){ 
     echo "\t(skipping empty item)\n"; 
     continue; // don't process empty items 
    } 
    $count++; 
    if ($count > 0){ 
     echo "-not the first line-\n"; 
    } 
    else 
     echo $item." is the first line\n"; 

    switch ($item){ 
     case "item1": 
      echo "item 1 found\n"; 
     break; 
    } 
} 

?> 

輸出

(skipping empty item) 
    (skipping empty item) 
John Doe is the first line 
    (skipping empty item) 
-not the first line- 
    (skipping empty item) 
    (skipping empty item) 
-not the first line- 
item 1 found 
+0

vardump對所有空字符串返回'string(0)「」'。即使這是問題,爲什麼它會在'switch'內工作? – aggregate1166877 2012-02-21 10:32:10

+0

@ user1166877我很困惑代碼的哪一部分存在問題,以及問題實際是什麼。你能展示一個你的代碼輸出的例子,然後輸出你期望的結果嗎? – JJJ 2012-02-21 10:35:47

+0

使用你的'continue'-block你的輸出語句只會**不會被執行** if'$ item ==「」'。在我的例子中'switch'-block中,輸出語句只會在** $ item ==「item1」'時被執行**。如果你的'$ item =「」'例如'continue'塊不會被執行,因爲'「」!=「」',但是「item1」-case也不會被執行,因爲'「」!= 「物品1」'。讓我們看看你的$ lines變量的一些例子,如果你不介意的話。 – Basti 2012-02-21 10:37:37