Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

number checker

hello there,
I would like to create something so that if I give a number lets say (150)
the code checks from 0 until 150 and once it finds it then say "number found!"
Avatar of nanharbison
nanharbison
Flag of United States of America image

this should work

<?PHP
$checkthisnumber = $_GET['thisnumber'];
 
$i=1;
while ($i < 151) 
{
	
 if ($i == $checkthisnumber) 
 	{
		echo "number found";
		break;
	}
$i++;
}
 
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nanharbison
nanharbison
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XK8ER

ASKER

yes, the last one is good.. how can I make it show a number not found?
Avatar of XK8ER

ASKER

lets say I type number 6 it should be like

1 not found
2 not found
3 not found
4 not found
5 not found
6 number found
Avatar of XK8ER

ASKER

ok I think I got it!
$checkthisnumber = '50';
 
$i=1;
while ($i < 151) 
{
        
 if ($i == $checkthisnumber) 
        {
                echo $checkthisnumber . ' number found';
                break;
        }
      			else
        				echo $i . ' not found<br>';
$i++;
}

Open in new window

like this

<?PHP
$checkthisnumber = $_GET['thisnumber'];
 
$i=1;
while ($i < 151) 
{
	
 if ($i == $checkthisnumber) 
 	{
		echo "number found, it is: ". $checkthisnumber;
	} else {
		echo $checkthisnumber." was not found ";
	}
$i++;
}
 
?>

Open in new window

oops, that was wrong

<?PHP
$checkthisnumber = $_GET['thisnumber'];
$checkthisnumber = 100;
$i=1;
while ($i < 151) 
{
	
 if ($i == $checkthisnumber) 
 	{
		echo "number found, it is: ". $checkthisnumber;
	} else {
		echo $i." was not found<br /> ";
	}
$i++;
}
 
?>

Open in new window

use the break statement if you want the loop to stop when the number is found.