這篇文章主要介紹了PHP計算個人所得稅,結合實例形式分析了php自定義函數不使用速算扣除數計算個人所得稅的相關操作技巧,涉及數組遍歷、數值運算的簡單使用,需要的朋友可以參考下
本文實例講述了PHP計算個人所得稅。分享給大家供大家參考,具體如下:
不使用速算扣除數計算個人所得稅,PHP自定義函數實現個人所得稅計算。使用速算扣除數計算個人所得稅過于簡單,略過不提。
PHP和JS有相同之處,知道PHP計算個人所得稅的方法以后,也可以同理寫出JS代碼個算個人所得稅。不同之處在于,javascript沒有foreach()
這樣的語法結構,不過隨著時代的變遷,現代瀏覽器中JS ECMASCRIPT 5也開始支持forEach()
方法了。
09 | function getPersonalIncomeTax( $salary , $deduction =0, $threshold =3500){ |
10 | if (! is_numeric ( $salary ) || ! is_numeric ( $deduction ) || ! is_numeric ( $threshold )){ |
13 | if ( $salary <= $threshold ){ |
16 | $levels = array (1500, 4500, 9000, 35000, 55000, 80000, PHP_INT_MAX); |
17 | $rates = array (0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45); |
18 | $taxableIncome = $salary - $threshold - $deduction ; |
20 | foreach ( $levels as $k => $level ){ |
21 | $previousLevel = isSet( $levels [ $k -1]) ? $levels [ $k -1] : 0; |
22 | if ( $taxableIncome <= $level ){ |
23 | $tax += ( $taxableIncome - $previousLevel ) * $rates [ $k ]; |
26 | $tax += ( $level - $previousLevel ) * $rates [ $k ]; |
28 | $tax = round ( $tax , 2); |
32 | echo getPersonalIncomeTax(10086.11); |
希望本文所述對大家PHP程序設計有所幫助。