newtophp2010 Posted October 6, 2011 Share Posted October 6, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/248533-placing-limits-on-variable-input-and-displacing-results-once-variable-is-entere/ Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 6, 2011 Share Posted October 6, 2011 You could do: $first = rand(1, 100); $second = rand(1, 100); ... and... echo $first; echo $second; ... where you need them. EDIT: I've never actually tried it. I got it from the PHP manual: http://php.net/manual/en/function.rand.php Quote Link to comment https://forums.phpfreaks.com/topic/248533-placing-limits-on-variable-input-and-displacing-results-once-variable-is-entere/#findComment-1276358 Share on other sites More sharing options...
MasterACE14 Posted October 6, 2011 Share Posted October 6, 2011 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. <?php 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' /> </form> FORM; } ?> Manual: - strip_tags() - isset() - Heredoc Quote Link to comment https://forums.phpfreaks.com/topic/248533-placing-limits-on-variable-input-and-displacing-results-once-variable-is-entere/#findComment-1276360 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.