Writing HTML inside echos VS Writing HTML between PHP tags

Between PHP Tags

if(condition){ ?> <!-- HTML here --> <? } 

In echos

 if(condition){ echo "HTML here"; } 

In my code, I use the above two methods. But I wonder which one is an industry standard and good for performance? Are there any pros and cons of the above methods? Which method should I use? and why?

Since I'm new to PHP, I hope you guys help me figure this out. Thanks

+4
source share
2 answers

Best industry practice tends to keep your HTML and your PHP completely separate (see http://www.smarty.net/ for an example template system)

However, you may find that your editor will do brighter syntax highlighting if you don't put your HTML in an echo.

+3
source

Personal preferences based on readability. Choose something that will be easier for you if someone else reads your code (or you in the future) to understand.

Typically, though, if there is a lot of HTML, you won’t want to put it in the PHP echo, and if it is short, you would. Think of the former as a template, and the latter as a method that returns an HTML fragment.

+2
source

Source: https://habr.com/ru/post/1444043/


All Articles