A few questions for people who understand well the work of the PHP interpreter.

  • Does the number of opening handles affect the speed of the script? Those. there is a mixture of HTML markup and PHP code in the file, will it significantly affect performance if we open and close <? ?> <? ?> on each line?

This:

 <? if( $val > 0 ) { echo 'yes'; } else { echo 'no'; } ?> 

or

 <? if( $val > 0 ) { ?> <? echo 'yes'; ?> <? } ?> <? else { ?> <? echo 'no'; ?> <? } ?> 
  • Is there a difference in the speed of execution by the interpreter, the classical syntax and the alternative?

Those. between so:

 <? if( $val > 0 ) { echo 'yes'; } else { echo 'no'; } ?> 

and so:

 <? if( $val > 0 ): echo 'yes'; else: echo 'no'; endif; ?> 

options.

  • Which of the output options of the variable and the results of the function uses less resources, if there is any difference at all?

This:

 <? echo '$value'; ?> 

or this one:

 <?=$value?> 
  • @Palmervan is it you have not seen themes for wordPress :) - Alex Kapustin
  • @shurik saw, but I’d rather not see it!))) - Palmervan
  • Yes, it is clear that in this form <? ?> meaningless, but this is an example. The question is whether the number of descriptors in the file affects the execution time. After all, the same code can be written using HTML code inserts directly, while closing the PHP script, or use echo. In addition, sometimes just for readability, it is convenient to increase the number of descriptors, but the question is how wasteful it is in terms of resources. - TomaZ
  • No matter how it is there - it is advisable to use a similar approach in templates, in the rest of the code IMHO - no, and under no circumstances, except for confusion in the code, you will not add anything. - Zowie

2 answers 2

 <? $e = ''; $st = microtime(1); for ($i = 0; $i < 1000000; $i++) { if (1 > 0) { echo $e; } else echo $e; } echo '<br />'.(microtime(1)-$st); $st = microtime(1); for ($i = 0; $i < 1000000; $i++) { ?> <? if (1 > 0) { ?> <? echo $e; ?> <? } else { ?> <? echo $e; ?> <? } ?> <? } echo '<br />'.(microtime(1)-$st); echo '<hr />'; $st = microtime(1); for ($i = 0; $i < 1000000; $i++) { ?> <? if (1 > 0) { ?> <?=$e?> <? } else { ?> <?=$e?> <? } ?> <? } echo '<br />'.(microtime(1)-$st); $st = microtime(1); for ($i = 0; $i < 1000000; $i++) { ?> <? if (1 > 0) { ?> <? echo $e; ?> <? } else { ?> <? echo $e; ?> <? } ?> <? } echo '<br />'.(microtime(1)-$st); echo '<hr />'; ?> 

Result:

 0.08100700378418 2.4613921642303 ---- 3.0508909225464 2.4576618671417 

The last 2 varied from "1.5 / 6" to "4/1", so they are probably the same. But the first couple - speaking)

UPDATE syntax

 <? $e = ''; $st = microtime(1); for ($i = 0; $i < 1000000; $i++) { if (1 > 0) { echo $e; } else echo $e; } echo '<br />'.(microtime(1)-$st); $st = microtime(1); for ($i = 0; $i < 1000000; $i++) { if (1 > 0): echo $e; else: echo $e; endif; } echo '<br />'.(microtime(1)-$st); echo '<hr />'; ?> 0.089139938354492 0.082063913345337 
  • In general, it is logical, then what is the point ?> <? echo 'a'; ?> <? ?> <? echo 'a'; ?> <? is obtained equivalent to echo ' '; echo 'a'; echo ' '; echo ' '; echo 'a'; echo ' '; + must also read this segment between ?> and <? . - Sh4dow pm
  • Well, yes, I agree that it is logical. It turns out that the "reversal" to the interpreter is not a gift for nothing, as one would expect. - TomaZ

In my opinion, this is "saving on matches" and hardly anyone will give an accurate and correct answer.

  • I still hope that there will be a guru =)) Perhaps that "at the matches", but as my experience shows, sometimes the little things turn out to be very important. - TomaZ 2:51 pm
  • Did not try to measure time? - avp
  • No, I have not tried it, but it seems to me that in order to see the difference, you need a large enough script, the same in functionality, but written in a different style. I still hope that there are people who know the answer to this question. - TomaZ