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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.