The problem I’m having is that PHP isn’t handling my continue correctly. My code is as follows:
$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;
}
}
The if ($count > 0) part executes regardless of where the continue is placed before it, but the switch executes as expected.
If I remove the if ($count > 0) and place it inside the switch, it executes as expected:
switch ($item){
case "item1":
if ($count > 0){
echo '-not the first line-'; // executes as expected
}
echo "item 1 found";
break;
}
Is this normal? Thanks
Your expectation is that it doesn’t execute? You’re setting $count to “-1”, then increment it with $count++. In my book, $count would then be 0, which is not greater than 0, which means the if condition is true?
EDIT: based on the information I got from the comment, you’re not using the contents of the first line, and you’re using $count merely to keep track of the number of iterations done? If so; what’s wrong with the following: