PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am doing an exercise called "i'm thinking of a number" and part of the script requires you print how many turns it has been. I cannot get the turns thing right - everything else is working. Can someone take a look at my code and tell me what i am doing wrong.
hi guys, i did a slight modification of this code.
once i enter some values for guess in the first field which is equal to $random , ie $guess = 1 , the program would go into the else part and print try again without getting the values for $random and $tryAgain.
and once i reenter some values again for guess , the program will go into the the if($guess == "") part and print print "Welcome!" and "$random.
how come the program treat $guess as an empty string ie $guess == "", even though i have entered some values for it ?
Your code is a little all over the place here. You've got two forms, one of which isn't closed. You're setting $count to whatever was sent back by the form (ie. $_POST['count']), then immediately overwriting that by setting $count to 0, and then immediately incrementing it to 1. You're then setting it back to 0 and incrementing again farther down in the code. Might make more sense to initialize your variables to some default values, then check if the form has been submitted and update those values accordingly, then, last, evaluate the submitted guess.
I presume that the 'isset' in your code does the same job?
The difference is really in the else clause. Your $count value is left uninitialized if $_POST['count'] is not set, which will generate an undefined variable notice when you try to use it in your form. Christopher's code initializes it to 0 if it wasn't already set by the form.
wendyj wrote:And, I have looked up 'intval' - and it seems to be a function that changes a number to an integer if it has a decimal place - is this correct?
intval() will convert any type of variable to an integer. This script expects an integer, so make sure it gets it. It is a good habit to validate and filter every input that comes from the user (or the web server). You want to make sure that someone cannot pass a value in count that is not an integer. In this case it many not cause a problem, but there clever ways to inject malicious things into your scripts though POST/GET variables.