[SOLVED] Why does this happen?

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!

Moderator: General Moderators

Post Reply
raging radish
Forum Commoner
Posts: 32
Joined: Sun Nov 14, 2004 3:02 pm
Location: Toronto

[SOLVED] Why does this happen?

Post by raging radish »

Sami | Help us, help you. Please use

Code: Select all

and

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.
Last edited by raging radish on Thu Nov 25, 2004 12:11 pm, edited 1 time in total.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
raging radish
Forum Commoner
Posts: 32
Joined: Sun Nov 14, 2004 3:02 pm
Location: Toronto

Post 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"
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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?
raging radish
Forum Commoner
Posts: 32
Joined: Sun Nov 14, 2004 3:02 pm
Location: Toronto

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
raging radish
Forum Commoner
Posts: 32
Joined: Sun Nov 14, 2004 3:02 pm
Location: Toronto

Post 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.
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post 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>
raging radish
Forum Commoner
Posts: 32
Joined: Sun Nov 14, 2004 3:02 pm
Location: Toronto

Post 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.
raging radish
Forum Commoner
Posts: 32
Joined: Sun Nov 14, 2004 3:02 pm
Location: Toronto

[SOLVED] Why does this happen?

Post 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.
Post Reply