What is the recommended way to embed html codes inside php codes?
Let's say I have 2 cases:
<?php print("html code goes here"); ?> vs
<?php ?> html codes goes here <?php ?> Will the performance for the PHP interpreter in the first case be worse than the second? (Due to additional processing overhead within the print function).
So, does anyone have a recommended way to embed html codes inside php codes?
Do not do this at all! Use a template system like Smarty so you can separate your logic from your displayed code. It also allows you to bring a designer who can work with html and not know php at all. Smarty templates are more like HTML than PHP, and this can make a big difference when working with a designer.
In addition, you do not need to worry about your designer messing with your code during the search / replace!
It would be even better to move on to setting up a Django or Rails type , which has a clearly defined code / template setting, but if you cannot do this (cheap hosting solutions), then at least use templates. I donβt know if smarty is the best for PHP, but it is much better than the solutions you offer.
Oh, for the sake of all those who later edit your code, never put any significant amount of HTML code inside the line and repeat it. In any language.
HTML belongs to HTML files formatted and styled in your IDE or editor, so you can read it. Giant string blocks are the biggest cause of HTML errors I've ever seen.
In this case, the performance should not be too large, but I assume that the second will be faster, because it is transmitted directly to the output or buffer.
If you want to make it easier to read, include short tags and write it as follows:
?><b>blah blah blah</b><? Also, with short tags enabled, variables are easier to output:
Hello, <?= $username ?> If you use this to create some kind of reusable library, there are other options.
Never put HTML inside PHP codes unless you specifically intend to do this or there is very little of it. But then again, 100% separation is what I recommend. People will have to work very hard to understand your code later if you accept them. Especially designers who may not like php.
The golden rule is that dividing the front and back of the process to the maximum helps in all aspects. Keep things where they belong. Styles in CSS, Java scripts in JS, Php in the library folder / files and just use the required classes / functions.
Use short tags <? If necessary (but I don't like: P) also the <?= tag for echo output. In addition, short tags are best avoided .
You should put HTML outside of PHP code to improve service and scalability. It is also very useful to do all the necessary data processing before displaying any data in order to separate the logic and presentation.
Instead of trying to think about the constant separation of your php and HTML, you should be in the mind, separating the backend logic and the display logic.
The MVC pattern is a good way to think about your code. To use PHP correctly, you must use the MVC pattern (model-view-controller)
[over the parapets] Many of us have studied templates from WordPress, where it is almost impossible to do anything without embedding php. I fully understand why people protect strict MVC or engines like Smarty, but the fact is that with WordPress development you need to manipulate the outputs on the fly with php. In fact, based on this background, I always believe that "hp" in php was for this reason. Therefore, I could write βnormalβ HTML code, process server processing a bit, and return to HTML.
So, from my point of view, the answer to your question is the second of your examples, which is much easier to read - one of the foundations of elegant coding. But it depends. If there is a lot of processing to create the simple html part, then it may be easier to build a large variable and repeat it at the end. I hold back a few lines of echo expressions. In this case, I will most likely use the function so that my HMTL is clean. Again, WordPress does this a lot; for example, the_title() returns a simple string, but does a transaction before returning that string, so <h1><? the_title(); ?> </h1> <h1><? the_title(); ?> </h1> <h1><? the_title(); ?> </h1> reads well.
This is the POV of a WordPress developer who never formally taught complex coding. I expect to lose a lot of reputation points over this answer. [/ over the parapet]