Using PHP shorthand and short_open_tag
I hope this will pursuade you to stop using short_open_tag syntax.<?= or <?
Reasons to NOT use PHP Shorthand syntax
- WordPress Coding Standards - Using php style short open tag syntax is EXPRESSLY forbidden in the WordPress Codex
No Shorthand PHP tags Important: Never use shorthand PHP start tags. Always use full PHP tags.
- WordPress VIP Coding Standards - They enforce the WordPress Coding Standards but much more strictly, claiming to go line by line in any plugin or theme to make sure.
- PEAR Coding Standards: http://pear.php.net/manual/en/standards.tags.php
Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. This is required for PEAR compliance and is also the most portable way to include PHP code on differing operating systems and setups.
- There is talk that
short_open_tag
and asp style tags will be DISABLED in php 6.They are already working to decouple <?= from short_open_tag for php 6
- PHP Best Practices and Recommendations (taken right from recommended php.ini comments).
- php < 5.2
Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.
- php > 5.3
It's been recommended for several years that you not use the short tag "short cut" and instead to use the full <?php and ?> tag combination. With the wide spread use of XML and use of these tags by other languages, the server can become easily confused and end up parsing the wrong code in the wrong context. But because this short cut has been a feature for such a long time, it's currently still supported for backwards compatibility, but we recommend you don't use them.
- php < 5.2
- Ever since php 5.1 was released, both the recommended and production sample php.ini files that come with the php release by default have short_open_tag set to OFF
php.ini values : short_open_tag PHP 4, 5_0 * Default behaviour : on * php.ini-dist : on * php.ini-recommended : on PHP 5_1, 5_2: * Default behaviour : on * php.ini-dist : on * php.ini-recommended : off PHP 5_3: * Default behaviour : on * php.ini-development : off * php.ini-production : off
- Parser Speed - As far as parser speed is concerned, as long as short_open_tag is set to on in the php.ini and not set using a runtime config like ini_set the actual parsing speed won't be slower. What is slower is the actual parsing of the document with short_open_tag turned on because the parser reads anything starting with <? instead of just <?php.
- Security, on all the sites and servers I administer, short_open_tag is ALWAYS disabled in the php.ini file by me. This prevents additional checking from having to take place for cleaning user-input, makes evalling code easier and safer (1 less thing to worry about), and keeps the parser from having a more wild acceptance of code it accepts.
- XML Errors, XSLT Translations, Validation, PHP Editors, color syntax highlighting Lets say in php you want to output an xml document (or parse .html files as php). Well if short_open_tag is turned on what happens to this code?
<?php // output xml header ?> <?xml version="1.0" encoding="utf-8"?>
Well that results in a PHP error: Parse error: syntax error, unexpected T_STRING in ... on line ...
The workaround is pretty simple, just do:
<?php // output xml header echo '<?xml version="1.0" encoding="utf-8"?>';
More Info from PHP RFC
Also read: https://wiki.php.net/rfc/shortags.
Other Reasons not to use PHP ShortHand
not making use of short tags, means your source code will also be a valid XML document, which allows you to do many things with it such as validation, XSLT translations, etc, as well as allowing your text editor to parse your code for syntax colouring. Every PHP tag will simply be interpreted as an XML processing instruction (commonly referred to as PI). The reason why all the other suggested methods are not advisable is because they contain the characters ?> inside the PHP tag, which the XML parser will interpret as the end of the processing instruction. A processing instruction is defined in XML as: PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' In other words, it explicitly forbids the characters ?> to occur together within a processing instruction, unless they are delimiting the end of the tag. It also requires a PITarget (an identifier starting with a letter) immediately after the initial start delimiter, which means that all short tag formats are also invalid XML. Following these guidelines will result in code that is portable to servers with any configuration and allow you perform many useful tasks on your XML or XHTML source documents. Even if you do not intend to validate or translate your source documents, and you can ignore some incorrect syntax colouring in your text editor, it is still best to get into good habits early.
Recommendation: Do not use PHP shorthand
So I recommend from now on use the full <?php ?> style. Here's one of my old bad coding habits, using a loop with processing in it instead of out of it:
<?php $array=array(); while (sizeof($array)>10) { }
instead of
<?php $array=array(); $count=sizeof($array); while ($count > 10) {}
Break The Habit
What I do to break the bad habit is I slap myself on both sides of my face when I notice I did it and immediately correct it after the slap. You may have a different bad-habit fixer, but feel free to use mine, it works really well for me :)
« Show or Hide WordPress Admin Bar PluginSUDO – sudoers Config File »
Comments