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:

$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:

$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 thoughts on “Closing PHP Tag – More Work”

  1. 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 :-)

  2. 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.

  3. 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.

  4. 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.

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *