Jump to content

Recommended Posts

Hi there, I'm very new to php and have been following a guide from www.learnphpfree.com I was reading through Lesson 7 although I still do not understand the $_GET and $_POST I continued with this script but adjusted it to suit me. here is my code

 

<form action="if.php" method="get"> <!--//Make the HTML Form -->
Your Number:<input name="num" type="text"> <!--//Make the input box -->
<input type="submit" value="Submit"> <!--//Make the Submit botton -->
</form> <!--// End the Form -->


<?php
/* Make a new variable called "$cpu_number" and set it to a random number. */
$cpu_number = rand(0, 100);

/* "if" somthing was posted (isset) do the following: */
if (isset($_GET['num'])) { 
$number = $_GET['num']; 
/*put the posted variable "num" into a new variable called "$number". */

If ($number > $cpu_number) {
/* "if" $number is greater than $cpu_number do the following: */
echo "Your Number Is Bigger! CPU's number is $cpu_number";
} elseif ($number > 100) {
echo "Please enter a number between 0 and 100";
}
If ($number < $cpu_number) {
/* "if" $number is less than $cpu_number do the following: */
echo "Your Number Is Smaller! CPU's number is $cpu_number";
}

if ($number == $cpu_number) {
echo "You picked the same number as me!";
}
/*if ($number > 100) {
echo "But please enter a number between 0 and 100";
}*/

}

 

I told the script to ignore my last if statement as it did work but with only one problem

If the number was over 100 it was still saying "Your number is bigger" but I wanted it to only say that if my number was bigger but not over 100

 

Could someone please help me :)  and if anyone has a better source to learn PHP I would be greatful, but I must warn you, I have zero knowledge of html or any other coding language so I really am starting from the very beginning on PHP.

Link to comment
https://forums.phpfreaks.com/topic/102987-basic-php/
Share on other sites

change order of if statemans

1st check is biger the 100

If ($number > 100) {
/* "if" $number is greater than $cpu_number do the following: */
echo "Please enter a number between 0 and 100";
} elseif ($number > $cpu_number) {
echo "Your Number Is Bigger! CPU's number is $cpu_number"; 
}

Link to comment
https://forums.phpfreaks.com/topic/102987-basic-php/#findComment-527579
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.