==
should be avoided; it attempts to coerce the types before running the equivalence test. So "6fjsdkjfds" == 6
happens to be true because the leading "6"
is cast to a number before the comparison: (int)"6fjsdkjfds" => 6
.
Always use ===
instead.
Now, this change breaks your code further and seems like a step in the wrong direction. But that's because using your set of numbers as an array and performing a lookup with in_array
(or array_key_exists
if you want O(1) lookup time using keys instead of values...) is the correct way to test membership, not scanning a string or using ==
or ===
.
Instead, try:
<?php
$skip = [6, 8, 9];
for ($i = 0; $i < 15; $i++) {
if (in_array($i, $skip)) {
continue;
}
echo "The number is: $i<br>";
}
?>
If $skip
(or $kecuali
) isn't data you have control over, you might need to parse it into an array with:
$skip = array_map("intval", explode(",", $kecuali));
before the above code.
If you have large arrays, consider improving the complexity to O(n+m)
instead of O(n*m)
with:
<?php
$skip = [6, 8, 9];
$skip = array_flip($skip);
for ($i = 0; $i < 15; $i++) {
if (array_key_exists($i, $skip)) {
continue;
}
echo "The number is: $i<br>";
}
?>