Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'echo' or drop out of 'programming' write HTML then start PHP code again

For the most part, when I want to display some HTML code to be actually rendered I would use a 'close PHP' tag, write the HTML, then open the PHP again. eg

<?php
// some php code
?>
<p>HTML that I want displayed</p>
<?php
// more php code
?>

But I have seen lots of people who would just use echo instead, so they would have done the above something like

<?php
// some php code
echo("<p>HTML that I want displayed</p>");
// more php code
?>

Is their any performance hit for dropping out and back in like that? I would assume not as the PHP engine would have to process the entire file either way.

What about when you use the echo function in the way that dose not look like a function, eg

echo "<p>HTML that I want displayed</p>"

I would hope that this is purely a matter of taste, but I would like to know if I was missing out on something. I personally find the first way preferable (dropping out of PHP then back in) as it helps draw a clear distinction between PHP and HTML and also lets you make use of code highlighting and hinting for your HTML, which is always handy.

like image 243
thecoshman Avatar asked Apr 18 '10 18:04

thecoshman


People also ask

Can you echo HTML in PHP?

Using echo or print: PHP echo or print can be used to display HTML markup, javascript, text or variables.

When PHP code inserted in the line of HTML code it is called?

Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content. This allows PHP to be embedded in HTML documents, for example to create templates.

How can I add HTML code in PHP page?

You can open a PHP tag with <? php , now add your PHP code, then close the tag with ?> and then write your html code.

Why PHP is not working in HTML?

Php files can always read and display HTML code, but HTML does not automatically parse php code. To do so, you will need to make adjustments to your . htaccess file. Once that is done, the php code will display within HTML files without issue.


2 Answers

The first type is preferable, exactly for the reasons you mentioned.

Actually, echoing out whole chunks of html is considered bad practice.

like image 137
K. Norbert Avatar answered Oct 04 '22 03:10

K. Norbert


No, there's no performance increase that would be visible.

Sometimes its just simply easier to output content using echo (for example, when inside a while or for loop) than to close the php tag.

like image 23
Eduardo Scoz Avatar answered Oct 04 '22 02:10

Eduardo Scoz