php如何避免否定答案

时间:2017-03-02 23:54:15

标签: php numbers tax negative-zero

您好我是一名新手并为税务目的编写一个简单的代码。到目前为止,我已经编写了一个小代码,但我认为函数taxable_i($i)似乎存在问题,这不会让我$taxable_i NIL。我尝试了一切。 当我尝试通过函数创建$taxable_i NIL时,它不会让我这样做。它显示了£13900的结果。我认为该计划正在回应$taxable_i而不是应付税款。 如果我将上述所有内容一起删除,那么应付税款将转到-£2199.80,这是负数。 请注意我放$taxable_i <personal_allowance以使答案为零,但结果不为零。我不知道为什么以及我做错了什么。 我们非常欢迎任何帮助。'

<?php
// VAT 
define("vat_threshold",83000 );
define("vat_rate",0.2 );

// RELIEFS
define("personal_allowance",11000 );

//RATES
define("starting_rate",0 );
define("basic_rate",0.2 );
define("higher_rate",0.4 );
define("add_rate",0.45 );
define("divs_ord_rate",0.075 );
define("divs_uuper_rate",0.325 );
define("divs_add_rate",0.381 );

// THRESHOLDS
define("savings_income_band",5000 );
define("basic_rate_band",32000 );
define("higher_rate_band",150000 );
define("divs_allowance",5000 );

define ("band_0",0);
define("band_1",11000);
define("band_2",32000);
define("band_3",100000);
define("band_4",122000);
define("band_5",150000);

function taxable_i($i) {

    if ($i <= band_1 ) {
        $taxable_i = $i * 0;
        return $taxable_i;
    }
    if ($i <= band_3 ) {
        $taxable_i = $i - personal_allowance;
        return $taxable_i;
    } 
    if ($i >band_3 && $i <=band_4) {
        $taxable_i = $i - (personal_allowance-($i - band_3)/2);
        return $taxable_i;
    }
    if ($i > band_4) {
        $taxable_i = $i;
        return $taxable_i;
    }

}

$starting_income = $i = 1;
echo $i;    

$taxable_i = taxable_i($i);
echo $taxable_i;

switch ($taxable_i) {

case ($taxable_i > band_5):
    $diff = $taxable_i - band_5;
    $tax5 = $diff * add_rate;
    $taxable_i = band_5;
    $diff = $taxable_i - band_2;
    $tax4 = $diff * higher_rate;
    $taxable_i = band_2;
    $tax3 = $taxable_i * basic_rate;
    $tax_payable = $tax5 + $tax4 + $tax3;
    break;

case ($taxable_i > band_2 && $taxable_i <= band_5 ):
    $diff = $taxable_i - band_2;
    $tax4 = $diff * higher_rate;
    $taxable_i = band_2;
    $tax3 = $taxable_i * basic_rate;
    $tax_payable = $tax4 + $tax3;
    break;

case ($taxable_i < band_2):
    $tax = $taxable_i * basic_rate;
    $tax_payable = $tax;
    break;

default:    
    $taxable_i <= band_0;
    $tax_payable == 0;
    break;
}

echo $tax_payable;
?>

1 个答案:

答案 0 :(得分:1)

您没有在case语句中添加条件,您应该在那里使用if/elseif/elseswitch/case的工作方式是将初始switch ()表达式的值与每个case表达式的值进行比较。所以它将$taxable_i$taxable_i > band_5的值进行比较,因此它将一个数字与truefalse进行比较。

switch块替换为:

if ($taxable_i > band_5) {
    $diff = $taxable_i - band_5;
    $tax5 = $diff * add_rate;
    $taxable_i = band_5;
    $diff = $taxable_i - band_2;
    $tax4 = $diff * higher_rate;
    $taxable_i = band_2;
    $tax3 = $taxable_i * basic_rate;
    $tax_payable = $tax5 + $tax4 + $tax3;
}

elseif ($taxable_i > band_2 ) {
    $diff = $taxable_i - band_2;
    $tax4 = $diff * higher_rate;
    $taxable_i = band_2;
    $tax3 = $taxable_i * basic_rate;
    $tax_payable = $tax4 + $tax3;
}

elseif ($taxable_i >= band_0) {
    $tax = $taxable_i * basic_rate;
    $tax_payable = $tax;
}

else {    
    $tax_payable == 0;
    break;
}