结合PHP和HTML

时间:2011-05-26 11:46:30

标签: php

作为一个新手,我的问题是在PHP中编写这样的代码,混合使用HTML和PHP还是有更好的方法来实现它是个好习惯

<?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>";
    }
?>

7 个答案:

答案 0 :(得分:1)

好问题。

我总是建议那些刚刚开始学习PHP的人,他们关心将标记和PHP脚本混合在一起并不是很重要,因为在开始时,你需要学习的是熟悉语法和看看PHP代码是如何工作的。

但是当你进一步改进时,将标记(HTML)与业务层(PHP脚本)分开是一种很好的做法,这样你的脚本看起来更干净,更好,更容易维护。

关于您上面的代码,我建议您在我的回复中查看此主题:How to connect controller to view in PHP OOP?

答案 1 :(得分:0)

要么是,要么打开和关闭php标签,在我看来这是丑陋且难以阅读的。我个人更喜欢回复来自php的html,就像你一样。

另一个选项,在某些情况下更干净,将输出保存到变量中并继续添加新字符串,然后在代码末尾回显它。 EJ:

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

总之,在每种场合使用更清洁,更容易阅读的内容。如果你做错了,你的代码看起来会很难看并且很难维护,这就是很多人讨厌php的原因。

答案 2 :(得分:0)

这是一个合理的开始。随着项目的增长,您可能需要考虑将标记与内容分离(例如,首先生成内容,然后将内容传递给以HTML格式标记的显示例程)。

答案 3 :(得分:0)

不,以这种方式开发并不是一个好习惯。最终,如果项目变大,代码会变得更加混乱,难以维护。

您最好使用模板引擎。

我对Smarty有很好的经验。请看一下:

http://www.smarty.net/

最初,你必须创建一些文件夹,但在那之后,它很容易。模板语言很容易理解。我甚至为非常小的项目使用它。

答案 4 :(得分:0)

尝试将逻辑与html分开,最好将逻辑保留在文档的开头。更好的是使用像Smarty这样的模板系统。

答案 5 :(得分: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
    }
?>

答案 6 :(得分: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代码并编写一个函数。