-2

Hi I am a newbe and writing a simple code for tax purposes. So far I have wrote a small code but I think there seems to be a problem with the function taxable_i($i) which is not letting me make $taxable_i NIL. I have tried everything. When I try to made $taxable_i NIL via function it wont let me do it. It shows me a result of £13900. I think the program is echoing $taxable_i rather then the tax payable. If I delete the above all together then the tax payable comes to -£2199.80, which is in minus. Please note that I put $taxable_i <personal_allowance so that the answer is zero but the result is not zero. I do not know why and what I am doing wrong. Any help is most welcome.'

<?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;
?>
4

1 回答 1

1

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

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;
}
于 2017-03-03T00:01:58.557 回答