jonesmeister Posted March 9, 2011 Share Posted March 9, 2011 <?php echo '<form action="hwCase.php" method="post"> Enter number <input type="text" name="number"/><br /> </form>'; $input = $_POST['number']; // if a number is entered in the form, ie not null if (isset($input)) { switch ($input) { // if number entered is less than 0, ie negative number case ($input<0): echo "Enter a positive number."; break; // if number entered is greater than 1000 case ($input>1000): echo "Enter a number less than 1000."; break; // if number entered is not a number, ie a letter, a character case (!ctype_digit($input)): echo "<br><img src=\"squidward.jpg\"><br><br>Enter a valid number. "; break; // if valid number is entered, loop "hello world" that many times case (($input>0) && ($input<1000)): echo "hello world " . $input . "<br />"; break; // if anything else is entered default: echo "Enter a number."; break; } } // if no number is entered in the form, ie null else { echo "Please enter a number."; } ?> For some reason, my loop doesn't work. Please help me fix it! Thanks in advance! I need to loop this part: case (($input>0) && ($input<1000)): echo "hello world " . $input . "<br />"; break; Link to comment https://forums.phpfreaks.com/topic/230117-loop-in-if-and-switch-statement/ Share on other sites More sharing options...
kenrbnsn Posted March 9, 2011 Share Posted March 9, 2011 When you're doing this type of switch <?php switch ($input) { // if number entered is less than 0, ie negative number case ($input<0): echo "Enter a positive number."; break; // if number entered is greater than 1000 case ($input>1000): echo "Enter a number less than 1000."; break; // if number entered is not a number, ie a letter, a character case (!ctype_digit($input)): echo "<br><img src=\"squidward.jpg\"><br><br>Enter a valid number. "; break; // if valid number is entered, loop "hello world" that many times case (($input>0) && ($input<1000)): echo "hello world " . $input . "<br />"; break; // if anything else is entered default: echo "Enter a number."; break; } } ?> you need to use <?php switch (true) ?> Ken Link to comment https://forums.phpfreaks.com/topic/230117-loop-in-if-and-switch-statement/#findComment-1185104 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.