Jump to content

Placing limits on variable input, and displacing results once variable is entere


Recommended Posts

Hello all. I am very new to PHP, and I am not sure where to look or what I'm looking for in my current assignment.


My task is to take in two numbers between 0-100. Once I take in that number, it should state beside it "The __ was accepted."

The program should not accept any numbers greater than 100 or any characters.


Once I do this, I must take a second number and do a similar thing.


Finally, I must have a statement show up at the bottom stating which number is greater.


Essentially, I need help in determining what I should use to place parameters, and how I can keep the program from echo ing any statement until input has been taken and tested for parameters.


Any help you can provide will be greatly appreciated!

Link to comment
Share on other sites

here's a dodgy way to get the job done, I'll leave it up to you to either learn from it, or modify it. Take note of the links I've added below. I've left out a few key things on purpose, as it's an assignment meaning you need to learn.

if(isset($_POST['num1']) && isset($_POST['num2'])) { // check if Form values are entered (form submitted)
  $num1 = strip_tags($_POST['num1']); // assign number 1 to a variable
  $num2 = strip_tags($_POST['num2']); // assign number 2 to a variable
if($num1 >= 0 && $num1 <= 100) { // check number 1 is 0-100
   echo "$num1 has been accepted.<br />";
   $r = 1;
} else {
   echo "$num1 has not been accepted.<br />";
if($num2 >= 0 && $num2 <= 100) { // check number 2 is 0-100
   echo "$num2 has been accepted.<br />";
   $t = 1;
} else {
   echo "$num2 has not been accepted.<br />";

if(isset($t) && isset($r)) {
   if($num1 > $num2) {
     echo 'number 1 is greater than number 2';
   elseif($num1 < $num2) {
     echo 'number 2 is greater than number 1';

} else { // if form has not been submitted or missing input, display the form...
   echo <<<FORM
<form action='' method='post'>
<input type='text' name='num1' />
<input type='text' name='num2' />
<input type='submit' />



- strip_tags()

- isset()

- Heredoc

Link to comment
Share on other sites


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.