php 如何避免否定答案
php how to avoid negative answer
嗨,我是新手,正在编写一个简单的税务代码。到目前为止,我已经写了一小段代码,但我认为函数 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;
?>
您不要在 case
语句中放置条件,您应该在那里使用 if/elseif/else
。 switch/case
的工作方式是将初始 switch ()
表达式的值与每个 case
表达式的值进行比较。所以它将 $taxable_i
与 $taxable_i > band_5
的值进行比较,因此它将一个数字与 true
或 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;
}
嗨,我是新手,正在编写一个简单的税务代码。到目前为止,我已经写了一小段代码,但我认为函数 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;
?>
您不要在 case
语句中放置条件,您应该在那里使用 if/elseif/else
。 switch/case
的工作方式是将初始 switch ()
表达式的值与每个 case
表达式的值进行比较。所以它将 $taxable_i
与 $taxable_i > band_5
的值进行比较,因此它将一个数字与 true
或 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;
}