Reputation: 605
As a newbie, my question is is it good practice to write code like this in PHP, mixing HTML and PHP or is there a better way of doing it
<?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>";
}
?>
Upvotes: 0
Views: 229
Reputation: 1529
Two suggestions:
1 - avoid nested code. try to write
if("bad condition") exit;
rather than
if("good condition")
{
<do the big job for many line of code>
....
}
This is a good coding practice
2- most of the time you can write plain html for main document structure and avoiding. And you only use echo for the inner tags which contains dynamic content.
<?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>
Next step would probably be to extract php code from the page and write a function.
Upvotes: 0
Reputation:
Good question.
I always recommend people who have just started learning PHP that it isn't very important for them to care about mixing markup and PHP script together, because at the beginning, what you need to learn is to get familiar with the syntax and see how PHP code works.
But when you improve to a further stage, it's a GOOD practice to separate markup (HTML) from business layer (PHP script), so that your script looks cleaner, nicer and easier to maintain.
About you code above, I would recommend you to have a look at this topic, in my reply: How to connect controller to view in PHP OOP?
Upvotes: 1
Reputation: 8079
It is either that, or opening and closing php tags, which in my opinion uis ugly and difficult to read. I personally prefer echoing the html from the php, like you did.
Another option, which is cleaner in some cases, would be saving the output into a variable and keep adding the new strings, then echoing it at the end of your code. Ej:
$output = "";
$world = "world";
$output.= "Hello";
if ($world) {
$output.= ' '.$world;
}
echo $output; //would print "hello world
In conclusion, use what is cleaner and easier to read in each occasion. If you do it wrong, your code will look ugly and will be difficult to maintain, which is the reason a lot of people hate php.
Upvotes: 0
Reputation: 12518
<?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
}
?>
Upvotes: 0
Reputation: 156
Try to keep the logic separate from the html, preferably keeping the logic at beginning of document. Even better use a template system like Smarty for example.
Upvotes: 0
Reputation: 2841
No, it isn't really good practice to develop this way. Eventually, if the project gets bigger, the code gets more messy and harder to maintain.
You better use a template engine.
I've good experiences with Smarty. Please take a look:
Initially, you have to create some folders, but after that, it's pretty easy. The template language is easy to understand. I use it even for very small projects.
Upvotes: 0
Reputation: 20036
This is a reasonable start. As your project grows, you may want to consider separating markup from content (for instance, generate the content first, then pass the content to a display routine which marks it up in HTML).
Upvotes: 0