PHP - 使代码更高效/专业

时间:2013-10-26 10:34:52

标签: php

我正在进行PHP任务,基本上我已经完成了所有工作。但是由于我对PHP的了解有限,代码看起来有点过于简单。它的作用是根据输入的小时数和工资计算总工资。

你们有什么建议可以让它更好/更短,效率更高吗?

<?php
$hours = $_GET ["hours"];
$wages = $_GET ["wages"];

if (empty($hours)) 
{
    echo "Amount of hours worked is required. <br />";
}
if (empty($wages)) 
{
    echo "Employee wage is required. <br />";
}

if (!is_numeric($hours) and ($wages) && !empty ($hours) and ($wages)) 
{
    echo "Please enter numeric numbers only. <br />";
}

if (!empty($hours) and ($wages)) 
{
    if (is_numeric($hours) and ($wages)) 
    {
         if ($hours <= 40) 
         {
        $totalPay = $hours * $wages;
        echo "Your total pay is $ $totalPay";
         }

         if ($hours > 40) 
         {
        $totalPay = ((40 * $wages) + (($hours - 40) * $wages * 1.5));
        echo "Your total pay is $ $totalPay";   
         }
    }
}       
else 
{
    echo "Unable to calculate total pay.";
}

&GT;

1 个答案:

答案 0 :(得分:0)

通过实施validate功能

,您可以更简单
function validate($time, $fee)
{
    if(!is_numeric($time) || empty($time)) // ...etc
    {
         return false;
    }

    return true;
} 

if(validate($_GET['hours'], $_GET['wages']))
{
    // do your calculation
}
else
{
    // display error
}