简体   繁体   中英

PHP If function does not work

i am new to this PHP and would like some suggestion why my calculation does not work.

$income = number_format($_POST[income], 2);
$year = $_POST[year];
if ($income >= 0 && $income <= 18200){ 
    $taxo = number_format(0,2);}
elseif ($income >= 18201 && $income <= 37000){
    $taxo = number_format($income * 0.19 ,2);
}

and somewhere down in my HTML file

tax on income
$
echo $taxo;                 

However when i run the file, $taxo is alwasy 0 (?) .can anyone please advise where i got it wrong?

Thanks in advance

Albert

number_format() returns a string, which you're trying to do integer comparisons on. eg

$income = '12345678';
$income = number_Format($income, 2); // 12,345,678.00

if ('12,345,678.00' >= 0) && ('12,345,678.00' <= 18200)

PHP will convert your string BACK to an integer, and you end up doing

if (12 >= 0) && (12 <= 18200)

number_format() is useful for human-readable output. It is utterly USELESS when you're doing internal-only comparisons, because you're taking what should be apples-to-apples comparisons, and turning them into oranges-to-apples.

$_POST[income]替换$_POST[income] $_POST['income'] ,将$_POST[year]替换$_POST['year']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM