Reputation: 75
I want to skip the number 10 so that i only have numbers from 15 to 5 without outputting number 10.
<?php
$x = 15;
while($x >=5) {
echo "$x <br>";
$x--;
}
?>
Upvotes: 4
Views: 75
Reputation: 89629
An other way without test:
$a = array_merge(range(15,11), range(9,5));
foreach($a as $num)
echo $num . '<br>';
Upvotes: 0
Reputation: 861
All the other posts are correct, but since you're learning, you should really learn without using shorthand, as it can sometimes be difficult to read.
Not everyone uses shorthand, and in most practices a coding standard is used. Ours in particular doesn't use shorthand.
Example:
<?php
$x = 15;
while($x >=5 ) {
if($x != 10){
echo $x . "<br />";
}
$x--;
}
?>
Upvotes: 1
Reputation: 389
<?php
for($i=15;$i>=5;$i--){
if($i == 10) continue;
echo $i . '<br>';
}
?>
or
<?php
for($i=15;$i>=5;$i--){
if($i != 10) echo $i . '<br>';
}
?>
Upvotes: 4
Reputation: 618
$num = 15;
while($num >=5 ) {
if($num != 10) echo $num . "\n";
$num--;
}
Upvotes: 0
Reputation: 1531
Add an if
condition to ignore $x
when it is equal to 10:
<?php
$x = 15;
while($x >=5 ) {
if($x != 10) echo $x . "<br>";
$x--;
}
?>`
Upvotes: 3