Solved

number checker

Posted on 2008-09-29
8
218 Views
Last Modified: 2012-05-05
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!"
0
Comment
Question by:XK8ER
  • 5
  • 3
8 Comments
 
LVL 17

Expert Comment

by:nanharbison
ID: 22595327
this should work

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

Open in new window

0
 
LVL 17

Accepted Solution

by:
nanharbison earned 500 total points
ID: 22595338
or if you want to show the number

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

Open in new window

0
 
LVL 1

Author Comment

by:XK8ER
ID: 22595352
yes, the last one is good.. how can I make it show a number not found?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:XK8ER
ID: 22595378
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
0
 
LVL 1

Author Comment

by:XK8ER
ID: 22595414
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

0
 
LVL 17

Expert Comment

by:nanharbison
ID: 22595423
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

0
 
LVL 17

Expert Comment

by:nanharbison
ID: 22595451
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

0
 
LVL 17

Expert Comment

by:nanharbison
ID: 22595477
use the break statement if you want the loop to stop when the number is found.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question