How can I find and print all of the numbers betwee

2019-07-15 12:13发布

Right now I'm asking the user for two numbers. I'm trying to print the numbers in between $one and $two assuming $one is smaller than $two.

标签: php range
5条回答
闹够了就滚
2楼-- · 2019-07-15 12:22

range gives an array containing all the numbers.

You can iterate over that:

foreach (range($one, $two) as $number)
    echo "$number <br>\n";

Or simply use a loop:

for ($number = $one; $number <= $two; $number++)
    echo "$number <br>\n";
查看更多
倾城 Initia
3楼-- · 2019-07-15 12:22

This sounds like homework...

for ($i=$one+1; $i<$two; $i++)
{
  echo $i . "\n";
}

This really gets you only the numbers between, not the endpoints.

查看更多
劫难
4楼-- · 2019-07-15 12:34

Just a simple for loop should do the trick:

for($i=$a; $i<=$b; $i++) {
  echo $i;
}
查看更多
霸刀☆藐视天下
5楼-- · 2019-07-15 12:41
<?php
foreach (range($one, $two) as $number) {
    echo $number." \n";
}
?>

range($one, $two) makes an array of numbers from $one to $two.

<?php
$numbers = range($one, $two);
foreach ($numbers as $number) {
    echo $number." \n";
}
?>

In this example, the array of numbers are first stored in $numbers before they are printed.

If $one is 5 and $two is 10 these examples will output:

5 
6 
7 
8 
9 
10 
查看更多
叼着烟拽天下
6楼-- · 2019-07-15 12:45
for($i=$one + 1; $i<$two; $i++) {
    echo $i;
}
查看更多
登录 后发表回答