PHP`继续`不按预期工作
我遇到的问题是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;
}
这是正常的吗?谢谢
如果($ 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";
}
}
@BarryLangerak:是的,但它需要检查字符串是否为空。如果字符串为空,则继续并忽略循环的其余部分。除了'count> 0'部分之外,所有其余的循环都被忽略。 'count> 0'被用来检查它是否是第一行,并在代码 – aggregate1166877 2012-02-21 10:35:31
啊中跟踪迭代次数。这有点解释。你有没有听说过'array_shift()'函数? – 2012-02-21 10:50:14
居然没有,还没有听说过'array_shift()';谢谢 – aggregate1166877 2012-02-21 12:42:46
即使您没有继续阻止,您的第二个示例仍然适用于您。
$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
vardump对所有空字符串返回'string(0)“”'。即使这是问题,为什么它会在'switch'内工作? – aggregate1166877 2012-02-21 10:32:10
@ user1166877我很困惑代码的哪一部分存在问题,以及问题实际是什么。你能展示一个你的代码输出的例子,然后输出你期望的结果吗? – JJJ 2012-02-21 10:35:47
使用你的'continue'-block你的输出语句只会**不会被执行** if'$ item ==“”'。在我的例子中'switch'-block中,输出语句只会在** $ item ==“item1”'时被执行**。如果你的'$ item =“”'例如'continue'块不会被执行,因为'“”!=“”',但是“item1”-case也不会被执行,因为'“”!= “物品1”'。让我们看看你的$ lines变量的一些例子,如果你不介意的话。 – Basti 2012-02-21 10:37:37
你能提供'$ lines'的内容是什么? – Vitamin 2012-02-21 10:18:45
继续在这种情况下只会执行,如果你的$ item是“”,是这样吗? – linuxeasy 2012-02-21 10:19:20
适合我:http://codepad.viper-7.com/cKTFYH – PeeHaa 2012-02-21 10:20:28