开发者

Dynamic HTML content through PHP

开发者 https://www.devze.com 2023-03-05 21:43 出处:网络
I\'ve heard it\'s a bad practice to echo dynamic HTML 开发者_开发百科content through PHP. But what are the alternatives, in case you need it, like, f.e. a navigation menu on a website, repeated in eve

I've heard it's a bad practice to echo dynamic HTML 开发者_开发百科content through PHP. But what are the alternatives, in case you need it, like, f.e. a navigation menu on a website, repeated in every page?


This is the reason for PHP's existence.

You heard wrong.


I suspect that this was meant as an advice against "hard-coding" the views in your PHP scripts, and rather separate your concerns by introducing something like an MVC approach. The gains in terms of maintainability and clarity of the code are significant, and in that respect it is a good advice.

That is really just an abstraction, though. At some point PHP has to output the HTML to the browser.

So, outputting dynamic HTML content is an integral part of PHP, but there are worse and better ways of doing it, and hard-coding HTML strings within echo statements is generally not the best way of doing it. Introduce a template engine and separate views and business logic, and you're likely better off.


It's not bad practice to echo content within html, it's bad practice to perform tasks such as the compilation of the data with the html, for example:

<?php

$query = mysql_query("SELECT * FROM table");
echo "<tably>"
while($row = mysql_fetch_assoc($query))
{
     ?><tr><td><?php echo $row['item']; ?></td></tr>
}
?>

That ugly and bad practice, what you would do is something like this:

<?php

$data = array();
try
{
    $query = mysql_query("SELECT * FROM table");
    while($row = mysql_fetch_assoc($query))
    {
        $data[] = $row;
    }
}catch(Exception $e)
{
     die("Database Error: " . $e->getMessage());
}

/*
     * Some more queries and logical data stuff.
     * Set headers as well
*/

require("main_template.php");

and then within the main template:

<table>
     <?php foreach($data as $row): ?>
     <tr>
         <td><?php echo $row['item']; ?></td>
     </tr>
     <?php endforeach;?>
</table>

The reasons we like to get al the required data for the page at the start is because if there are any errors triggered we can display a nice page, instead of errors being thrown half way down your content and what so on.


it is always better to split presentation and script, for example instead of using code something like this

echo "<title>$title</title>";

you could use

<title><?php echo $title; ?></title>

consider splitting html code from php, keep php only for returning some set of results not actual html. this way it will be more readable IMO. also it doesn't matter whichever way you adopt because PHP was meant to return text and that's what you are doing.


If you have common code like a menu you can put the code for menu in aseparate menu.php file, and use the menu code using PHP include statement. refer here Php common menu code

Definitely what you have heard is not correct regarding PHP refer this article....

0

精彩评论

暂无评论...
验证码 换一张
取 消