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