Closing PHP Tag – More Work

I was testing PHP code with VLD and noticed something odd. If I left off the final closing PHP then there were fewer ops. Here is an example:

<?php
$first_name = 'Joseph';
$last_name = 'Scott';
?>

has 6 ops:

line     # *  op                  operands
------------------------------------------
   2     0  >   EXT_STMT                                                 
         1      ASSIGN           !0, 'Joseph'
   3     2      EXT_STMT                                                 
         3      ASSIGN           !1, 'Scott'
   5     4      EXT_STMT                                                 
         5    > RETURN              1

The same file, minus the closing PHP tag:

<?php
$first_name = 'Joseph';
$last_name = 'Scott';

has only 5 ops:

line     # *  op                operands
-----------------------------------------
   2     0  >   EXT_STMT                                                 
         1      ASSIGN        !0, 'Joseph'
   3     2      EXT_STMT                                                 
         3      ASSIGN        !1, 'Scott'
   4     4    > RETURN         1

I trimmed the VLD output to make it easier to read.

Boils down to an extra EXT_STMT op when the closing PHP tag is included.

12 Comments

  1. Does that translate into a (ubersubminimal, probably) speed / memory improvement?

  2. That one op is likely a very, very, VERY small amount of work. However, I subscribe to the view that doing less work to accomplish the same result is better performance. So from that point of view, yes, this should result in an ever so tiny improvement. For most folks that improvement is so small they would likely have a hard time measuring it :-)

  3. Nice simple find, but not recommended for more complicated sites. You can use that technique for inclusion files, since the include command is already nested in open/close tags.

  4. The closing PHP tag is optional, it doesn’t matter how complicated your site is.

  5. I have been excluding the closing PHP tag for a while after realizing it was optional. I now read the PHP open tag as “Start doing PHP stuff” and the PHP closing tag as “Start outputting stuff” — as opposed to “Stop doing PHP stuff”.
    If you’re not going to output any “stuff” at the bottom of your PHP script, you might as well exclude the closing tag.

  6. Good way to look at it.

  7. So, by giving PHP one less thing to read, it has one less thing to do? >_>

  8. Leaving off the closing tag also helps with the use of response headers later in the execution. If the closing tag was used its very easy to accidentally add some whitespace after it and cause the output to stop the headers being set.

    Also helps to avoid the issue of unwanted whitespace when using includes.

  9. How do you know what EXT_STMT is doing? There is no documentation on it anywhere as far as I can see.

  10. Unfortunately I don’t have a good answer for you. http://php.net/manual/en/internals2.opcodes.ext-stmt.php mentions it, but doesn’t provide any details.

  11. Nice formatting on the PHP example code! Is that a plugin, or part of your WordPress theme?

  12. I am using the SyntaxHighlighter Evolved plugin – http://wordpress.org/plugins/syntaxhighlighter/

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2014 Joseph Scott

Theme by Anders NorenUp ↑