简体   繁体   中英

Direct HTML or PHP generated html

What is better / faster:

For example:

STATIC / direct HTML:

<?php
for($i=0;$i<$sth;$i++) {
?>
<tr>
<td>
<?php echo $content;  ?>
</td>
</tr>
<?php
}
?>

OR

PHP generated HTML:

<?php

for($i=0;$i<$sth;$i++) {

echo "<tr><td>".$content."</td></tr>";

}

?>

Does it matter which option i choose?

Thanks

It's not so much a matter of speed which may vary based on use case, but of making the code clean and maintainable. Actually both examples make for code that's hard to maintain and read. I'd suggest using a simple and lightweight templating engine to separate all logic from presentation.

I think that there is no substantial difference between the two, the question should be "Which one is more readable" IMHO and i think that using php and html inline is far less readable than echoing php. But that's just my idea.

The better: Generated html. Generated html with php is far more easy to maintain and easier to read.

The faster: There is no significant speed difference. However on large dynamic websites where content is loaded from a database etc things might take a fraction of a second more time to output. However, the time you spend on updating a static html file is a lot more than editing dynamic content..

Go dynamic:]

In this case "PHP generated HTML" would be quicker because you are only doing one echo where as in "STATIC / direct HTML" you are doing $sth echos. If $sth is zero then "STATIC / direct HTML" would be quicker.

But seriously, the page is parsed and optimised/normalised so it doesn't make any difference. Parsing with less might be quicker because there are less context switches but this is the smallest part (compared to running it) so it makes negligible difference.

Just pick the style that you feel comfortable with.

Two codes represent the same thing, not differentiate in the speed, But the second code may be a little faster because the code does not contain more than one entry and exit signs.

<?php ?>

this will be carried out faster.

The first approach should be faster as it does not involve a lot of string concatenation. It's also better in terms of code readability.

I think the first solution:

It is clearer and do not require php elaborations with string to dispplay simple static content

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM