2013-10-26 31 views
-2

我正在從事一項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."; 
} 

?>

+4

我認爲這個問題將是對[codereview.se] :) –

+1

使用正確的縮進更合適的更簡單,它會使它看起來好很多 – aurbano

+0

簡單有什麼問題?哦,對,[KISS](https://en.wikipedia.org/wiki/KISS_principle)現在的意思是「保持超級複雜」。 – Oswald

回答

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 
} 
+0

謝謝你的提示,凱拉! 我用一個函數來檢查錯誤嘗試,但它繼續顯示一個空白頁沒有奏效。將再次嘗試!:) – user2922696