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:
[sourcecode lang=”php”]
<?php
$first_name = ‘Joseph’;
$last_name = ‘Scott’;
?>
[/sourcecode]
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:
[sourcecode lang=”php”]
<?php
$first_name = ‘Joseph’;
$last_name = ‘Scott’;
[/sourcecode]
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 replies on “Closing PHP Tag – More Work”
Does that translate into a (ubersubminimal, probably) speed / memory improvement?
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 🙂
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.
The closing PHP tag is optional, it doesn’t matter how complicated your site is.
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.
Good way to look at it.
So, by giving PHP one less thing to read, it has one less thing to do? >_>
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.
How do you know what EXT_STMT is doing? There is no documentation on it anywhere as far as I can see.
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.
Nice formatting on the PHP example code! Is that a plugin, or part of your WordPress theme?
I am using the SyntaxHighlighter Evolved plugin – http://wordpress.org/plugins/syntaxhighlighter/