From: richy at smilingsouls dot net Operating system: Windows XP Home PHP version: 4.3.4RC3 PHP Bug Type: Math related Bug description: Comparision operation fails
Description: ------------ In the following example from the Wrox Beginning PHP 4 textbook there is a simple loan formula calculation. (This example has been altered to accommodate register_globals = off and XHTML compliance) When one enters an age of 23, A salary of over 50,000 And a loan of 10,000 The loan application makes a few calculations and based on that input should come up with a 'loan approval'. However in the first conditional a comparison operation of less than or equal to is performed between a string ($_POST["Loan"]) and a double ($LoanAllowance), the comparision fails and evaluates to false when it is in fact true! Here is an output of the code: 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 The application prints out the values of each value being compared, 'if (10000 <= 10000)' Obviously this should cause the first boolean conditional to evaluate as true. 10000 is equal to 10000! We can see here that the value printed is the expected result and not a result of a syntax or logic error. In order to force this conditional statement to evaluate as true (for this case) a ceil operation must be preformed on the $LoanAllowance variable. However this appears to be overkill as $LoanAllowance is not a decimal or fraction value and even if it were, 10000 is still equal to 10000! This bug was first brought to my attention through the Wrox publishing p2p forums. And the original thread may be viewed here: http://p2p.wrox.com/topic.asp?TOPIC_ID=5869 It appears to only present this problem when the user selects an age of 23. And to my knowledge it has been reproduced at least three times. Reproduce code: --------------- <html> <head> <!-- test.php --> </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> Expected result: ---------------- (output from the php relevant portion) Namllu Credit Bank Loan Application Form Loan wanted 10000 Loan amount we will allow: 10000 if (10000 <= 10000) Yes, Juan Valdez, we are delighted to accept your application Actual result: -------------- (output from the php relevant portion) 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 -- Edit bug report at http://bugs.php.net/?id=26046&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=26046&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=26046&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=26046&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=26046&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=26046&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=26046&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=26046&r=support Expected behavior: http://bugs.php.net/fix.php?id=26046&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=26046&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=26046&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=26046&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=26046&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=26046&r=dst IIS Stability: http://bugs.php.net/fix.php?id=26046&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=26046&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=26046&r=float
