问题描述
||
作为一个新手,我的问题是在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>\";
}
?>
解决方法
好问题。
我总是建议刚开始学习PHP的人不要担心将标记和PHP脚本混合在一起是很重要的,因为一开始,您需要学习的是熟悉语法并了解如何PHP代码有效。
但是,当您进行进一步改进时,将标记(HTML)与业务层(PHP脚本)分开是一种很好的做法,以使您的脚本看起来更简洁,更美观,更易于维护。
关于上面的代码,我建议您在答复中查看此主题:如何连接控制器以在PHP OOP中进行查看?
,这是一个合理的开始。随着项目的发展,您可能需要考虑将标记与内容分开(例如,首先生成内容,然后将内容传递给显示例程,以HTML对其进行标记)。
,不,以这种方式开发并不是真正的好习惯。最终,如果项目变大,代码将变得更加混乱并且难以维护。
您最好使用模板引擎。
我在Smarty方面有很好的经验。请看一下:
http://www.smarty.net/
最初,您必须创建一些文件夹,但是此后,这非常简单。模板语言很容易理解。即使是很小的项目,我也使用它。
,尝试将逻辑与html分开,最好将逻辑放在文档开头。甚至最好使用像Smarty这样的模板系统。
,
<?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
}
?>
,要么就是那样,要么打开和关闭php标签,在我看来这是丑陋且难以阅读的。我个人更喜欢像从PHP那样回显php中的html。
在某些情况下更干净的另一种选择是将输出保存到变量中并继续添加新字符串,然后在代码末尾回显该字符串。 Ej:
$output = \"\";
$world = \"world\";
$output.= \"Hello\";
if ($world) {
$output.= \' \'.$world;
}
echo $output; //would print \"hello world
总之,在每种情况下都使用更清洁,更容易阅读的内容。如果做错了,您的代码将很难看并且难以维护,这就是很多人讨厌php的原因。
,两个建议:
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 . \",\" . $outstanding_balance . \"<br />\";
echo \"<tr> <td>\" . round ($principal_balance,2) . \"</td> </td>\";
$principal_balance = $outstanding_balance;
}
}
}
?>
</table>
</body>
</html>
下一步可能是从页面提取php代码并编写一个函数。