Jump to content

Basic PHP


ProLogiC

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

Archived

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.