Solved

number checker

Posted on 2008-09-29
8
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Technology Partners: 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!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

719 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