Request Dev Services »

Loop through a range with PHP, set min and max points

The PHP code below is taking a start-point and end-point, inserted by the user from HTML mark-up input fields; the while loop determines if the start-point and end-point provided by the user is acceptable, if it is, it will iterate through each value – the class Constants examples underneath illustrate another method, of using Constants to determine or set MIN and MAX values. Additionally, on the front-end side, you can use this type of mark-up to set min and max. 

 <input type="number" name="startpoint" min="100000" max="999998">
 <input type="number" name="endpoint" min="100001" max="999999">
    $begin = $_POST['startpoint'];
    $end = $_POST['endpoint'];
    $current_start = $begin;
    $num1 = rand(1,360);
    $num2 = rand(1,360);
    while ($begin < $end && $begin >= 100000 && $begin <= 999998 && $end >= 100001 && $end <= 999999) {
        for($i = $begin; $i <= $end; $i++) {
            echo '<span>Star SC ' . $i . ' mag ' . $num1 .', '. $num2;
    define('MIN_VALUE', '100000');
    define('MAX_VALUE', '999999');
    class Constants {
      const MIN_VALUE = $begin;
      const MAX_VALUE = $end;  
      public static function getMinValue()
        return self::MIN_VALUE;
      public static function getMaxValue()
        return self::MAX_VALUE;

About the Author
Cameron Cashwell Web Developer
I build websites, web apps, and software. Wanna work together? Let's chat about your project!

Leave a Reply

Notify of