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!
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I'm working through Ch 10 of "Teach Yourself PHP" and come across something I don't understand. The exercise involves creating simple web form which calls itself, the code is:
<?php
$num=42;
$message="";
if ( ! isset( $_POST['guess'])){
$message="Welcome to the guessing machine";}
else if ( $_POST['guess'] > $num){
$message= $_POST['guess']." is too big. Try a smaller number";}
else if ( $_POST['guess'] < $num){
$message= $_POST['guess']." is too small. Try a bigger number";}
else {$message="well done!";}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1 -strict.dtd">
<html>
<head>
<title>number guessing script</title>
</head>
<body>
<div>
<h1><?php print $message ?></h1>
<form method="post" action="<?php print $_SERVER['PHP_SELF']?>">
<p>Type you guess here: <input type="text" name="guess"/>
<input type="submit" value="submit"/>
</p>
</form>
</div>
</body>
</html>
It works correctly in every regard, except the message output is: "XXguess=XX is too big. Try a smaller number" where "XX" is the input number. Is adding the additional "guess=XX" normal or expected behaviour? If so, why.
I'm running Apache/PHP 4.2.2 locally on RH9.
Thanks.
Last edited by raging radish on Thu Nov 25, 2004 12:11 pm, edited 1 time in total.
well first it checks if the user has submitted a guess, if so, then:
it checks if the num is more than 42, if so, it sais "num too big"
if the num is not too big, then it checks if its too small. if so, then it sais 'num too small'
but if the number is not too big, AND its not too small, it must be correct, so it sais 'well done'
Sorry, I'm not sure if I made myself clear. I know that the calculation works as intended, I just can't see why or how the script would output the wording that it does.
From what I understand, the output wording should be: "XX is too big. Try a smaller number" rather than what it is actually doing, whch is "XXguess=XX is too big. Try a smaller number"
raging radish wrote:Sorry, I'm not sure if I made myself clear. I know that the calculation works as intended, I just can't see why or how the script would output the wording that it does.
From what I understand, the output wording should be: "XX is too big. Try a smaller number" rather than what it is actually doing, whch is "XXguess=XX is too big. Try a smaller number"
it shouldnt do that. have you modified the code from what you have posted?
I think I learned from the same book. I'm with refeld. The code you posted shouldn't do that. Post the code you are currently using and don't forget to use the php formatting tags.
else if ( $_POST['guess'] < $num){
$message= $_POST['guess']." is too small. Try a bigger number";}
where it refers (for lack of a better description) to $_POST['guess'] twice, whereas what you posted does not. This book covers PHP 4.3 with references as well to PHP 5 so maybe it's a version thing (running 4.2.2).
After encountering the problem in other scripts, I tried those same scripts on 2 other remote servers - the results there were error free and therefore the problem was something with my machine. Searching redhat, I came across an update for php to correct, among other issues, the errors I was getting.