...

View Full Version : Resolved if with or + continue: Newbie question



PonchoX
03-22-2010, 07:34 AM
hi.

learning PHP.

this code works fine:



for ($count = -3; $count <= 10; $count++) {
if ($count == 5) {
continue;
}
echo $count . ", ";
}

As expected, this outputs:

-3, -2, -1, 0, 1, 2, 3, 4, 6, 7, 8, 9, 10,

So how come I get nothing (no error, but no output) if I use 'or', like so:



for ($count = -3; $count <= 10; $count++) {
if ($count == 5 || 6) {
continue;
}
echo $count . ", ";
}


I would expect it to output -3 to 10, skipping 5 and 6.

abduraooft
03-22-2010, 09:07 AM
To get the desired output, you need to use

if ($count == 5 ||$count== 6) {
continue;
}

Your code
if ($count == 5 || 6) {
continue;
} is equivalent to

if ($count == 5 || true) {
continue;
} , which will evaluate to true, irrespective of the value of $count.

PonchoX
03-22-2010, 06:24 PM
Thank-you, kindly.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum