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
($Lolowance), 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
$Lolowance variable. However this appears to be overkill as
$Lolowance 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:
[url]http://p2p.wrox.com/topic.asp?TOPIC_ID=5869[/url]

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);
$Lolowance = $SalaryAllowance * $AgeAllowance;

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

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

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

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

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

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 [url]http://bugs.php.net/?id=26046&edit=1[/url]
--
Try a CVS snapshot (php4): [url]http://bugs.php.net/fix.php?id=26046&r=trysnapshot4[/url]
Try a CVS snapshot (php5): [url]http://bugs.php.net/fix.php?id=26046&r=trysnapshot5[/url]
Fixed in CVS: [url]http://bugs.php.net/fix.php?id=26046&r=fixedcvs[/url]
Fixed in release: [url]http://bugs.php.net/fix.php?id=26046&r=alreadyfixed[/url]
Need backtrace: [url]http://bugs.php.net/fix.php?id=26046&r=needtrace[/url]
Try newer version: [url]http://bugs.php.net/fix.php?id=26046&r=oldversion[/url]
Not developer issue: [url]http://bugs.php.net/fix.php?id=26046&r=support[/url]
Expected behavior: [url]http://bugs.php.net/fix.php?id=26046&r=notwrong[/url]
Not enough info: [url]http://bugs.php.net/fix.php?id=26046&r=notenoughinfo[/url]
Submitted twice: [url]http://bugs.php.net/fix.php?id=26046&r=submittedtwice[/url]
register_globals: [url]http://bugs.php.net/fix.php?id=26046&r=globals[/url]
PHP 3 support discontinued: [url]http://bugs.php.net/fix.php?id=26046&r=php3[/url]
Daylight Savings: [url]http://bugs.php.net/fix.php?id=26046&r=dst[/url]
IIS Stability: [url]http://bugs.php.net/fix.php?id=26046&r=isapi[/url]
Install GNU Sed: [url]http://bugs.php.net/fix.php?id=26046&r=gnused[/url]
Floating point limitations: [url]http://bugs.php.net/fix.php?id=26046&r=float[/url]