How to optimize this short script in php - salary calc

纵饮孤独 提交于 2019-12-12 03:36:23

问题


please help me to shorten the code if it si possible. It is working correctly but to echo all the elements of the salary I have to repeat this:

$pensjaBrutto = $_GET['pensjaBrutto'];

Is it possible to omit this repetition?

here is the code:

    <form method="get" action="">
    <meta charset="UTF-8">
    <label>Wprowadź kwotę zarobków <u>brutto</u></label>
    <br>
    <input type="text" name="pensjaBrutto" value="<?=(isset( $_GET['pensjaBrutto'] ))?$_GET['pensjaBrutto']:''?>"/>
    <br>
    <input type="submit" />
</form>

<?php
    if( isset( $_GET['pensjaBrutto'] ) ){
        echo "Wynagrodzenie brutto: PLN ".$pensjaBrutto = $_GET['pensjaBrutto'];

        $pensjaNetto = $pensjaBrutto *=0.703;
        echo "<br>Wynagrodzenie netto: $pensjaNetto PLN";

        $pensjaBrutto = $_GET['pensjaBrutto'];
        $sklzdr = $pensjaBrutto *=0.078;
        echo "<br>Skł zdrowotna: $sklzdr PLN"; 

        $pensjaBrutto = $_GET['pensjaBrutto'];
        $ubechor = $pensjaBrutto *=0.025;
        echo "<br>Ubez chorobowe: $ubechor PLN"; 

        $pensjaBrutto = $_GET['pensjaBrutto'];
        $uberent = $pensjaBrutto *=0.015;
        echo "<br>Ubez rentowe: $uberent PLN"; 

        $pensjaBrutto = $_GET['pensjaBrutto'];
        $ubeemer = $pensjaBrutto *=0.098;
        echo "<br>Ubez emerytalne: $ubeemer PLN"; 

        $pensjaBrutto = $_GET['pensjaBrutto'];
        $zalpit = $pensjaBrutto *=0.081;
        echo "<br>Zaliczka na PIT: $zalpit PLN"; 

    }

?>

回答1:


Like this?

if(isset($_GET['pensjaBrutto']))
{
    echo "Wynagrodzenie brutto: PLN ".$_GET['pensjaBrutto'];

    $calculations = array(
        'Wynagrodzenie netto'=>.703,
        'Skł zdrowotna'=>.078,
        'Ubez chorobowe'=>.025,
        'Ubez rentowe'=>.015,
        'Ubez emerytalne'=>.098,
        'Zaliczka na PIT'=>.081
    );

    foreach($calculations as $k=>&$v)
    {
        echo "<br>$k: ".((float)$_GET['pensjaBrutto'] * $v)." PLN";
    }
    unset($v);
}



回答2:


Don't use the *= operator and you'll be fine. So instead of

$ubeemer = $pensjaBrutto *=0.098

Instead use

$ubeemer = $pensjaBrutto * 0.098;

The difference is that the *= operator tells the script to multiply $pensjaBrutto by .098, assigning that new value to $pensjaBrutto. If you use the * operator instead, it will return the value of $pensjaBrutto * 0.098, allowing you to assign that to $ubeemer, but without changing the original value of $pennsjaBrutto.

In other words, replace all occurrences of *= with *, and you should be fine.



来源:https://stackoverflow.com/questions/35948084/how-to-optimize-this-short-script-in-php-salary-calc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!