2011-05-26 55 views
0

作爲一個新手,我的問題是,是這樣寫的PHP代碼是很好的做法,混合HTML和PHP還是有這樣做的更好的辦法結合PHP和HTML

<?php 
    if (isset($_POST['submit'])) 
    { 
     $principal_balance = $_POST['principal_amount']; 
     $interest_rate = $_POST['interest_rate']; 
     $repayment_amount = $_POST['repayment_amount']; 

     echo "<html>"; 
     echo "<head>"; 
     echo "<title> Loans </title>"; 
     echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />"; 
     echo" <link rel=\"stylesheet\" type=\"text/css\" href=\"styles/document.css\" />"; 
     echo "<body>"; 


     echo "<table>"; 
     echo "<th> Principal Balance </th> <th> Interest Amount </th> <th> Principal Balance Amount Recovered </th> <th> Principal Balance </th> <th> Outstanding Balance </th>"; 
     while ($principal_balance > 0) 
     { 

      if ($principal_balance < $repayment_amount) 
      { 
       exit; 
      } 
      else 
      { 
       $interest_amount = $interest_rate * $principal_balance * 1/12; 

       $principal_amount_recovered = $repayment_amount - $interest_amount; 

       $outstanding_balance = $principal_balance - $principal_amount_recovered; 

       round ($interest_amount, 2); 
       round ($principal_amount_recovered, 2); 
       round ($outstanding_balance, 2); 
       //echo $principal_balance . "," . $interest_amount . "," . $principal_amount_recovered . "," . $outstanding_balance . "<br />"; 
       echo "<tr> <td>" . round ($principal_balance, 2) . "</td> <td>" . round ($interest_amount, 2) . "</td> <td>" . round ($principal_amount_recovered, 2). "</td> <td>" . round ($outstanding_balance, 2) . "</td> </td>"; 

       $principal_balance = $outstanding_balance;   
      } 
     } 
     echo "</table>"; 
     echo "</body>"; 
     echo "</html>"; 
    } 
?> 
+0

對於像這樣的獨立腳本,沒有真正的問題。當您開始製作更大更復雜的腳本時,您需要考慮某種組織,例如[MVC](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%% 93控制器) – Ross 2011-05-26 11:49:14

回答

1

好問題。

我總是推薦那些剛剛開始學習PHP的人,他們關心將標記和PHP腳本混合在一起並不是非常重要,因爲在開始時,您需要學習的是熟悉語法並查看PHP代碼的工作原理。

但是,當您進一步改進時,將標記(HTML)與業務層(PHP腳本)分開是一種很好的做法,以便腳本看起來更乾淨,更好,更易於維護。

關於你上面的代碼,我建議你看一下這個話題,我的回答是:How to connect controller to view in PHP OOP?

0

它要麼是,或者打開和關閉php標籤,這在我看來很醜陋,難以閱讀。我個人更喜歡在PHP中迴應HTML,就像你一樣。

另一個選項在某些情況下更清晰,它會將輸出保存到變量中,並繼續添加新字符串,然後在代碼結尾處回顯該字符串。 Ej:

$output = ""; 
$world = "world"; 
$output.= "Hello"; 
if ($world) { 
$output.= ' '.$world; 
} 
echo $output; //would print "hello world 

總之,使用什麼更清潔,更容易閱讀在每個場合。如果你做錯了,你的代碼會看起來很醜並且很難維護,這就是很多人討厭php的原因。

0

這是一個合理的開始。隨着項目的發展,您可能需要考慮從內容中分離標記(例如,首先生成內容,然後將內容傳遞給以HTML標記它的顯示例程)。

0

不,這樣開發並不是很好的做法。最終,如果項目變得更大,代碼會變得更加混亂和難以維護。

您最好使用模板引擎。

我跟Smarty有過很好的經歷。請看看:

http://www.smarty.net/

首先,你必須創建一些文件夾,但在那之後,它很容易。模板語言很容易理解。我甚至用於非常小的項目。

+0

請不要使用聰明,它是緩慢和笨拙的 – 2011-05-26 11:57:57

+0

這是你的意見還是你有實際的數字? – edwin 2011-05-26 12:55:31

+0

這是我的觀點根據我的經驗。我忘了我另一件夾克上的數字 – 2011-05-26 13:04:40

0

儘量保持與html分離的邏輯,最好將邏輯保持在文檔的開頭。例如,更好地使用像Smarty這樣的模板系統。

0
<?php 
    if (isset($_POST['submit'])) 
    { 
     $principal_balance = $_POST['principal_amount']; 
     $interest_rate = $_POST['interest_rate']; 
     $repayment_amount = $_POST['repayment_amount']; 
?> 

<html> 
<head> 
<title> Loans </title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<link rel="stylesheet" type="text/css" href="styles/document.css" /> 
<body> 


<table> 
     <th> Principal Balance </th> <th> Interest Amount </th> <th> Principal Balance Amount Recovered </th> <th> Principal Balance </th> <th> Outstanding Balance </th> 
<?php 
     while ($principal_balance > 0) 
     { 

      if ($principal_balance < $repayment_amount) 
      { 
       exit; 
      } 
      else 
      { 
       $interest_amount = $interest_rate * $principal_balance * 1/12; 

       $principal_amount_recovered = $repayment_amount - $interest_amount; 

       $outstanding_balance = $principal_balance - $principal_amount_recovered; 

       round ($interest_amount, 2); 
       round ($principal_amount_recovered, 2); 
       round ($outstanding_balance, 2); 
       //echo $principal_balance . "," . $interest_amount . "," . $principal_amount_recovered . "," . $outstanding_balance . "<br />"; 
       echo "<tr> <td>" . round ($principal_balance, 2) . "</td> <td>" . round ($interest_amount, 2) . "</td> <td>" . round ($principal_amount_recovered, 2). "</td> <td>" . round ($outstanding_balance, 2) . "</td> </td>"; 

       $principal_balance = $outstanding_balance;   
      } 
     } 
?> 

</table> 
</body> 
</html> 
<?php 
    } 
?> 
0

兩個建議:

1 - 避免嵌套代碼。嘗試寫

if("bad condition") exit; 

而不是

if("good condition") 
    { 
    <do the big job for many line of code> 
    .... 
    } 

這是一個良好的編碼習慣

2 - 大多數時候,你可以寫主文檔結構,避免純HTML。而且你只對包含動態內容的內部標籤使用echo。

<?php 
    if (! isset($_POST['submit'])) 
    exit; 
?>   
<html> 
<head> 
<title> Loans </title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
    <link rel="stylesheet" type="text/css" href="styles/document.css" />" 
<body> 
<table> 
    <th> Principal Balance </th> 
    <th> Interest Amount </th> <th> Principal Balance Amount Recovered </th> 
    <th> Principal Balance </th> <th> Outstanding Balance </th> 

<?php 
$principal_balance = $_POST['principal_amount']; 
$interest_rate = $_POST['interest_rate']; 
$repayment_amount = $_POST['repayment_amount']; 

while ($principal_balance > 0) 
{ 

    if ($principal_balance < $repayment_amount) 
    { 
    exit; 
    } 
    else 
    { 
    $interest_amount = $interest_rate * $principal_balance * 1/12; 
     $principal_amount_recovered = $repayment_amount - $interest_amount; 

    $outstanding_balance = $principal_balance - $principal_amount_recovered; 

    round ($interest_amount, 2); 
    round ($principal_amount_recovered, 2); 
    round ($outstanding_balance, 2); 
    //echo $principal_balance . "," . $interest_amount . "," . $principal_amount_recovered . "," . $outstanding_balance . "<br />"; 
    echo "<tr> <td>" . round ($principal_balance, 2) . "</td> <td>" . round ($interest_amount, 2) . "</td> <td>" . round ($principal_amount_recovered, 2). "</td> <td>" . round ($outstanding_balance, 2) . "</td> </td>"; 

    $principal_balance = $outstanding_balance;   
} 
    } 
} 
?> 
</table> 
</body> 
</html> 

下一步可能是從頁面中提取php代碼並編寫一個函數。