Page 1 of 1
[SOLVED] Why does this happen?
Posted: Sun Nov 14, 2004 3:19 pm
by raging radish
Sami | Help us, help you. Please use Code: Select all
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:
Code: Select all
<?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.
Posted: Sun Nov 14, 2004 3:33 pm
by rehfeld
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'
the behaviro should be correct
Posted: Sun Nov 14, 2004 3:53 pm
by raging radish
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"
Posted: Sun Nov 14, 2004 8:22 pm
by rehfeld
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?
Posted: Mon Nov 15, 2004 11:43 am
by raging radish
rehfeld wrote:
it shouldnt do that. have you modified the code from what you have posted?
No, the code is exactly as shown in the book.
Posted: Mon Nov 15, 2004 12:12 pm
by neophyte
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.
Posted: Mon Nov 15, 2004 5:48 pm
by raging radish
neophyte wrote: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.
Here's the code, direct copy/paste of the file contents:
Code: Select all
<?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's exactly the same as what I posted earlier, except for the proper tags, sorry 'bout that folks.
Posted: Mon Nov 15, 2004 6:45 pm
by ecaandrew
try
Code: Select all
<html>
<head>
<title>number guessing script</title>
</head>
<body>
<?php
$num=42;
if ( ! isset( $_POST['guess'])){
echo "Welcome to the guessing machine";
} elseif ( $_POST['guess'] > $num){
echo "You guessed to high. Try a smaller number";
} elseif ( $_POST['guess'] < $num){
echo "You guessed to low. Try a bigger number";
} else {
echo "That is correct!";
exit;
}
?>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
Type you guess here: <input type="text" name="guess"/><br />
<input type="submit" value="submit"/><br />
</form>
</body>
</html>
Posted: Mon Nov 15, 2004 9:22 pm
by raging radish
Yeah, that returns the expected text.
I suppose it's getting confused at this part:
Code: Select all
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).
Thanks all.
[SOLVED] Why does this happen?
Posted: Thu Nov 25, 2004 11:33 am
by raging radish
Problem solved:
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.
http://bugzilla.redhat.com/bugzilla/sho ... i?id=82967
Everything seems to work as expected now.