PHP: loop from range 3 to 50 with 0.2 step

Started by evejones, Aug 13, 2022, 02:56 AM

Previous topic - Next topic

evejonesTopic starter

Could you provide instructions on creating a loop with a range of 3 to 50 and increment of 0.2?
  •  

offka

It appears that there are two code examples presented, one using a for loop and the other using the range function, to display a sequence of numbers from 3 to 50 with an increment of 0.2 in PHP.

 It is generally recommended to use integer values when creating loops; however, there may be instances where a non-integer step size is necessary. In cases such as these, it may be necessary to adjust the loop termination condition to account for possible floating-point precision issues.
Additionally, in situations where it is necessary to work with non-integer sequences, such as going from 30 to 500 and then dividing by 10, one can either divide by 10 directly or add it to a separate variable.

<?php

echo "<xmp>";

for(
$i 3$i <= 50$i 0.2)
{
    echo 
"{$i}\r\n";
}

echo 
"</xmp>";

?>


<?php

$arr 
range(3500.2);

foreach (
$arr as $i)
    {
        echo 
$i' ';
    }  
?>

  •  

friv10games

Like this?
for($iteration=3;$iteration<=50;$iteration+=0.2) {
foreach($names as $name) { }
}
  •