Should I wrap it all inside <? Php?> Using PHP?

Is there any difference between the following 2?

1 separate php code from html

<html> <body> <?php // php code 1 ... ?> <div> ... </div> <?php // php code 2 ... ?> <div> ... </div> <?php // php code 3 ... ?> </body> </html> 

2 everything inside php

 <?php echo "<html>"; ehco "<body>"; // php code 1 ... echo "<div> ... </div>"; // php code 2 ... echo "<div> ... </div>"; // php code 3 ... echo "</body>"; echo "</html>"; ?> 

Which is faster?

+4
source share
6 answers

This is not a way to increase speed. If you are not satisfied with the performance of your application, take the profiler and find the slowest part. After that, optimize this slowest part.

The way I optimize what is easy to optimize is always the worst.

+4
source

The first one is faster, because the interpreter will only care about the code inside the tags.

The second should not be used, double quotes are interpreted to see if there is any inside. You should always use a simple quote if you do not want the string to be interpreted.

+1
source

Normal use is what you specified in the first form, although in some cases, for example, a helper function to generate <a href="..." ... >link word</a> , you will generate HTML inside PHP code. Or if you have a function that generates a table, or ul with many li printed with data from an array or hashes.

+1
source

Regardless of performance differences, you will find it controversial and academic. Select the first form. The second form is not future. Simply changing the layout of the HTML page will require a software engineer. The first will just need a creative designer.

+1
source

If the operation cache code is installed on the server (see APC, eAccelerator), this does not matter in any case, since only the page should be interpreted at the first access.

0
source

Performance:

Perhaps the second example will be a bit slower because it makes more function calls, and also when the string is inside double quotes, then the interpreter will look for variables to replace and escape sequences, for example. \ n

PHP blocks should also be parsed. It may not matter, your script is precompiled / run on the accelerator.

Formatting:

In the first example, HTML will be generated that is automatically indented. The second example will lead to the creation of code that will be displayed on one line. This is not a problem, as it can be fixed.

Resolution:

Perhaps the first example is more readable. For instance. IDE can highlight HTML syntax and separately for PHP

Others:

A second example is likely to be a preferable choice if the code is in a structure and the markup is extracted into a smaller function (s), subclass, etc.?

0
source

All Articles