Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 30th, 2003, 11:29 AM
Registered User
 
Join Date: Oct 2003
Location: , , USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to brandonsaxe
Default Chp3, pg 100. Strange behavior

I have entered the code for the loan.html and loan.php files in chapter 3. I get strange behavior, however.
Enter age of 23.
Enter salary over $50k
Select loan amount of 10,000

Result:
Loan wanted:10000
Loan amount we will allow:10000

Sorry, , we cannot accept your application at this time

Doesn't 10000 <= 10000 hold TRUE?

The code in question is in loan.php:

if ($Loan <= $LoanAllowance) echo "Yes, $FirstName $LastName, we are delighted to accept your application";
if ($Loan > $LoanAllowance) echo "Sorry, $FirstName $LastName, we cannot accept your application at this time";

With $Loan = 10000 and $LoanAllowance = 10000 then shouldn't the first IF be TRUE and print "Yes..." ?

I tried this both on a Red Hat 9 with apache
and WinXP using CAMP (Cygwin Apache MySQL PHP).

The behavior is same on both.

Will someone out there try this combination and explain why the logical comparison is being done the way it is?

It is strange, I've tried all kinds of other ages and it works as expected.

Thanks,
Brandon
Reply With Quote
  #2 (permalink)  
Old October 30th, 2003, 01:49 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many of us don't have the book nor are we looking at the source code. Are you having problems with other scripts, or just this one? If it's just this one, feel free to post some code and let us know what your php configuration settings are (specifically, register_globals and error_reporting).

Also, since your two conditional statements are negations of each other (there isn't a single value for $loan that would pass or fail BOTH tests), you should streamline your code and use "else" instead of a 2nd "if".

if ($Loan <= $LoanAllowance)
{
   echo "Yes...";
}
else
{
   echo "Sorry...";
}


Things to look for: Make sure that your values are being submitted and retrieved. If register_globals is on, perhaps your loan amounts are reverting to some default values that prevent the loan from being processed.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #3 (permalink)  
Old October 30th, 2003, 02:32 PM
Registered User
 
Join Date: Oct 2003
Location: , , USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to brandonsaxe
Default

register_globals is ON

I have checked all the values using echo statements and all seems to be fine.

I guess I can try to change the two IFs to an IF ELSE statemnt and see, but I do not see how that should change the TRUE or FALSE value of the condition in question.


Here is the source for the exercise as downloaded from wrox website from chapter 3:

loan.html-
<HTML>
<HEAD></HEAD>
<BODY>
<B>Namllu Credit Bank Loan Application Form</B>
<FORM METHOD=POST ACTION="loan.php">
First Name:
<INPUT NAME="FirstName" TYPE="Text">
Last Name:
<INPUT NAME="LastName" TYPE="Text">
Age:
<INPUT NAME="Age" TYPE="Text"SIZE="3">
<BR>
<BR>
Address:
<TEXTAREA NAME="Address" ROWS=4 COLS=40>
</TEXTAREA>
<BR>
<BR>
What is your current salary?
<SELECT NAME="Salary">
<OPTION VALUE=0>Under $10000</OPTION>
<OPTION VALUE=10000>$10,000 to $25,000</OPTION>
<OPTION VALUE=25000>$25,000 to $50,000</OPTION>
<OPTION VALUE=50000>Over $50,000</OPTION>
</SELECT>
<BR>
<BR>
How much do you want to borrow?<BR><BR>
<INPUT NAME="Loan" TYPE="Radio" VALUE=1000>Our $1,000 package at 8.0% interest
<BR>
<INPUT NAME="Loan" TYPE="Radio" VALUE=5000>Our $5,000 package at 11.5% interest
<BR>
<INPUT NAME="Loan" TYPE="Radio" VALUE=10000>Our $10,000 package at 15.0% interest
<BR>
<BR>
<INPUT TYPE=SUBMIT VALUE="Click here to Submit application">
<INPUT TYPE=RESET VALUE="Reset application form">
</FORM>
</BODY>
</HTML>

And here is loan.php-

<HTML>
<HEAD></HEAD>
<BODY>
<B>Namllu Credit Bank Loan Application Form</B>
<BR>
<BR>
<?
$SalaryAllowance = $Salary/5;
$AgeAllowance = ($Age/10 - ($Age%10)/10)-1;
$LoanAllowance = $SalaryAllowance * $AgeAllowance;
echo "Loan wanted:$Loan<BR>";
echo "Loan amount we will allow:$LoanAllowance<BR><BR>";
if ($Loan <= $LoanAllowance) echo "Yes, $FirstName $LastName, we are delighted to accept your application";
if ($Loan > $LoanAllowance) echo "Sorry, $FirstName $LastName, we cannot accept your application at this time";
?>
</BODY>
</HTML>
Reply With Quote
  #4 (permalink)  
Old October 30th, 2003, 03:15 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I think Wrox hired an orangutan to write this code! It's so friggin sloppy.

Don't follow in the path of these sloppy coders:
1.) Don't use short-tags <? ?>
2.) See my HTML FAQ: http://p2p.wrox.com/topic.asp?TOPIC_ID=4028

Personally when I find a comparison that doesn't do what I expect it to, I output the values that I am comparing.

echo "if ($Loan <= $LoanAllowance)<br />\n";

Register Globals is all fine and dandy to get through a few of the books examples, but be warned you should move away from it and learn how to write the code with it off!

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #5 (permalink)  
Old October 30th, 2003, 03:45 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Well I'm a little confused.

I ran the code and got the same f'ed up error.

I rewrote the code for register_globals to be off:
Code:
<html>
<head>
</head>
<body>
<span style='font-weight: bold;'>Namllu Credit Bank Loan Application Form</span>
<form method='post' action='/test.php'>
First Name: <input name='FirstName' type='text' /><br />
Last Name: <input name='LastName' type='text' /><br />
Age: <input name='Age' type='text' size='3' />
<br /><br />

Address:
<textarea name="Address" rows='4' cols='40'></textarea>
<br />
<br />
What is your current salary?
<select name="Salary">
    <option value='0'>Under $10000</option>
    <option value='10000'>$10,000 to $25,000</option>
    <option value='25000'>$25,000 to $50,000</option>
    <option value='50000'>Over $50,000</option>
</select>
<br />
<br />
How much do you want to borrow?<BR><BR>
<input name='Loan' type='Radio' value='1000'>Our $1,000 package at 8.0% interest</input><br />
<input name='Loan' type='Radio' value='5000'>Our $5,000 package at 11.5% interest</input><br />
<input name='Loan' type='Radio' value='10000'>Our $10,000 package at 15.0% interest</input><br /><br />

<input type='submit' name='do_action' value='Click here to Submit application' />
<input type='reset' value='Reset application form' />
</form>

<br />
<br />
<?php

if (isset($_POST["do_action"]))
{

    echo "<span style='font-weight: bold;'>Namllu Credit Bank Loan Application Form</span><br /><br />";

    $SalaryAllowance  = $_POST["Salary"] / 5;
    $AgeAllowance     = (($_POST["Age"] / 10 - ($_POST["Age"] % 10 ) / 10) -1);
    $LoanAllowance    = $SalaryAllowance * $AgeAllowance;

    echo "Loan wanted {$_POST["Loan"]}<br />";
    echo "Loan amount we will allow: $LoanAllowance<br /><br />";

    echo "if ({$_POST["Loan"]} <= $LoanAllowance)<br />\n";

    if ($_POST["Loan"] <= $LoanAllowance) 

        echo "Yes, {$_POST["FirstName"]} {$_POST["LastName"]}, we are delighted to accept your application";

    else if ($_POST["Loan"] > $LoanAllowance)

        echo "Sorry, {$_POST["FirstName"]} {$_POST["LastName"]}, we cannot accept your application at this time";

}
?>
</body>
</html>
Output:
Namllu Credit Bank Loan Application Form

Loan wanted 10000
Loan amount we will allow: 10000

if (10000 <= 10000)
Sorry, Juan Valdez, we cannot accept your application at this time

Well I'm at a loss on this one? 10000 is equal to 10000, but the application doesn't think so! I tried setting the second conditional to else or else if, and yet the first conditional still evaluates to false.

What the hell is wrong with it Nik?

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #6 (permalink)  
Old October 30th, 2003, 03:50 PM
Authorized User
 
Join Date: Jul 2003
Location: , , Russia.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I checked that out on the wrox downloaded code. It is very strange...

I added some code to see what was going on:

$SalaryAllowance = $Salary/5;
$AgeAllowance = ($Age/10 - ($Age%10)/10)-1;
echo "age is $Age<br>";
$div_age = $Age/10;
echo "age divided by 10 is $div_age<br>";
$mod_age = $Age%10;
echo "age mod 10 is $mod_age<br>";
$mod_age_div = $mod_age/10;
echo "dividing the mod 10 number by 10 results in $mod_age_div<br>";
$allowed = $div_age - $mod_age_div -1;
echo "The age allowance is $allowed<br>";
echo "So the LoanAllowance = SalaryAllowance of $SalaryAllowance times $allowed<br>";
$LoanAllowance = $SalaryAllowance * $AgeAllowance;
echo "Loan wanted: $Loan<BR>";
echo "Loan amount we will allow: $LoanAllowance<BR><BR>";
echo "The Loan wanted is $Loan<br>";

/*
* these are the 3 tests I added
*/

//if ($Loan = $LoanAllowance) echo "The Loan of $Loan equals the LoanAllowance of $LoanAllowance<br>";
//if ($Loan < $LoanAllowance) echo "The Loan of $Loan is less than the LoanAllowance of $LoanAllowance<br>";
//if ($Loan > $LoanAllowance) echo "The Loan of $Loan is greater than the LoanAllowance of $LoanAllowance<br>";

if ($Loan <=$LoanAllowance) echo "The less than or equal to sign breaks only when you are 23 years old";
if ($Loan > $LoanAllowance) echo "Well, $FirstName $LastName, too bad.";

I uncommented test one (testing only for equality) and then age 23 worked. The other two "pretests" have no affect. So, testing for equality prior to testing for "less than or equal to" will *fix* the problem.

Don't know why it's breaking tho.

Here's some sample output from the code with the equality test put in before the "less than or equal" to test:
--------------------------

age is 22
age divided by 10 is 2.2
age mod 10 is 2
dividing the mod 10 number by 10 results in 0.2
The age allowance is 1
So the LoanAllowance = SalaryAllowance of 10000 times 1
Loan wanted: 10000
Loan amount we will allow: 10000

The Loan wanted is 10000
The Loan of 10000 equals the LoanAllowance of 10000
The less than or equal to sign breaks only when you are 23 years old

--------------------------

age is 23
age divided by 10 is 2.3
age mod 10 is 3
dividing the mod 10 number by 10 results in 0.3
The age allowance is 1
So the LoanAllowance = SalaryAllowance of 10000 times 1
Loan wanted: 10000
Loan amount we will allow: 10000

The Loan wanted is 10000
The Loan of 10000 equals the LoanAllowance of 10000
The less than or equal to sign breaks only when you are 23 years old


I keep forgetting that my memory is slipping.
Reply With Quote
  #7 (permalink)  
Old October 30th, 2003, 03:55 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Well I'm not sure why but I think that this has something to do with rounding... Why 10000 and 10000 would output without decimal places, I don't know, but when I inserted a rounding function it worked.

Code:
if (ceil($_POST["Loan"]) <= ceil($LoanAllowance)) 

    echo "Yes, {$_POST["FirstName"]} {$_POST["LastName"]}, we are delighted to accept your application";
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #8 (permalink)  
Old October 30th, 2003, 04:01 PM
Registered User
 
Join Date: Oct 2003
Location: , , USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to brandonsaxe
Default

Thank you rich for you insights about globals_on . I just started PHP coding this week and am only using this book as guide to start. However, I hve read about security with globals_on and will begin changing my code to use the $_GET and $_POST variables.

Security is a good thing ;).

The errata explains that as well. I have downloaded that and stuck it inside my book ;).

Also, thank you for the FAQ on HTML. It's on my bookmarks list now and I'll study that. I like being standards compliant and that will be a great way to accomplish that.

Thanks for taking the effort to put the code into your installation. It is interesting that you have gotten the same error.

It stumped me at first. I am just learning PHP, so I thought it was my own doing. This kind of thing does concern me, however, for future programs I build. If these conditions do not work, how can it be trusted?

I'm not highly concerned, but it's just in the back of my head.

Actually, I already enjoy programming with PHP and can't wait to write my own web apps with it utilizing PostgreSQL and MySQL databases.
Reply With Quote
  #9 (permalink)  
Old October 30th, 2003, 04:10 PM
Registered User
 
Join Date: Oct 2003
Location: , , USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to brandonsaxe
Default

Hi Oregon,

Forgive me (and correct me) if I'm wrong, but should there be two equal signs on the first test like this:

//if ($Loan == $LoanAllowance) echo "The Loan of $Loan equals the LoanAllowance of $LoanAllowance<br>";

and not this:

//if ($Loan = $LoanAllowance) echo "The Loan of $Loan equals the LoanAllowance of $LoanAllowance<br>";

The first test will always return TRUE since it is an assignment statement.

Brandon


Quote:
quote:Originally posted by Oregon
 I checked that out on the wrox downloaded code. It is very strange...

I added some code to see what was going on:

$SalaryAllowance = $Salary/5;
$AgeAllowance = ($Age/10 - ($Age%10)/10)-1;
echo "age is $Age<br>";
$div_age = $Age/10;
echo "age divided by 10 is $div_age<br>";
$mod_age = $Age%10;
echo "age mod 10 is $mod_age<br>";
$mod_age_div = $mod_age/10;
echo "dividing the mod 10 number by 10 results in $mod_age_div<br>";
$allowed = $div_age - $mod_age_div -1;
echo "The age allowance is $allowed<br>";
echo "So the LoanAllowance = SalaryAllowance of $SalaryAllowance times $allowed<br>";
$LoanAllowance = $SalaryAllowance * $AgeAllowance;
echo "Loan wanted: $Loan<BR>";
echo "Loan amount we will allow: $LoanAllowance<BR><BR>";
echo "The Loan wanted is $Loan<br>";

/*
* these are the 3 tests I added
*/

//if ($Loan = $LoanAllowance) echo "The Loan of $Loan equals the LoanAllowance of $LoanAllowance<br>";
//if ($Loan < $LoanAllowance) echo "The Loan of $Loan is less than the LoanAllowance of $LoanAllowance<br>";
//if ($Loan > $LoanAllowance) echo "The Loan of $Loan is greater than the LoanAllowance of $LoanAllowance<br>";

if ($Loan <=$LoanAllowance) echo "The less than or equal to sign breaks only when you are 23 years old";
if ($Loan > $LoanAllowance) echo "Well, $FirstName $LastName, too bad.";

I uncommented test one (testing only for equality) and then age 23 worked. The other two "pretests" have no affect. So, testing for equality prior to testing for "less than or equal to" will *fix* the problem.

Don't know why it's breaking tho.

Here's some sample output from the code with the equality test put in before the "less than or equal" to test:
--------------------------

age is 22
age divided by 10 is 2.2
age mod 10 is 2
dividing the mod 10 number by 10 results in 0.2
The age allowance is 1
So the LoanAllowance = SalaryAllowance of 10000 times 1
Loan wanted: 10000
Loan amount we will allow: 10000

The Loan wanted is 10000
The Loan of 10000 equals the LoanAllowance of 10000
The less than or equal to sign breaks only when you are 23 years old

--------------------------

age is 23
age divided by 10 is 2.3
age mod 10 is 3
dividing the mod 10 number by 10 results in 0.3
The age allowance is 1
So the LoanAllowance = SalaryAllowance of 10000 times 1
Loan wanted: 10000
Loan amount we will allow: 10000

The Loan wanted is 10000
The Loan of 10000 equals the LoanAllowance of 10000
The less than or equal to sign breaks only when you are 23 years old


I keep forgetting that my memory is slipping.
Reply With Quote
  #10 (permalink)  
Old October 30th, 2003, 04:38 PM
Authorized User
 
Join Date: Jul 2003
Location: , , Russia.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yup. My bad. My head is now back in my (*).

BTW, the == pretest doesn't fix anything, so please ignore everything. Looks like the rounding error is the issue, like Q says.

I keep forgetting that my memory is slipping.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Behavior With Anchor in XSLT kwilliams XSLT 6 July 21st, 2005 02:52 PM
STRANGE behavior..SQL Help skotman Classic ASP Databases 7 June 6th, 2004 03:55 PM
Strange behavior of DateTimePicker? wwz VS.NET 2002/2003 0 February 19th, 2004 06:56 AM
Please help with strange file download behavior! glwatson Classic ASP Basics 0 September 10th, 2003 08:57 AM



All times are GMT -4. The time now is 07:59 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.