Jump to content

Loop in If and Switch Statement


jonesmeister

Recommended Posts

	<?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
Share on other sites

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
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.