This wiki is for the exclusive use of my friends and colleagues. Account creation and anonymous editing have been turned off.

If you are a friend or colleague of mine, and would like to participate in this project, please send me a message.

If you find these pages useful, please donate to help cover server costs. Thanks!

Difference between revisions of "PHP start and end tags"

From OdleWiki
Line 32: Line 32:
 
</code>
 
</code>
  
 +
Because both short tags and ASP-style tags require changes to the server configuration, it's best to use standard tags to ensure that your code is portable and reusable.
 +
 +
Additionally, <span class="mono"><?</span> tells an [[XML]] parser to expect a processing instruction and is often included in XML documents. Therefore, if you intend to include XML in your script, you should avoid using short tags because the PHP engine is likely to confuse XML processing instructions and PHP start tags. If you intend to incorporate XML in your document, you should disable short tags.
 
[[Category:PHP]]
 
[[Category:PHP]]

Revision as of 23:13, 10 December 2013

When using PHP, you must tell the PHP engine that it has to parse the PHP. To do so, enclose your PHP code in PHP start and end tags.

There are four types of tags available: standard tags short tags, ASP tags, and script tags.

Tag Style Start Tag End Tag
Standard tags <?php ?>
Short tags <? ?>
ASP tags <% %>
Script tags <script language="php"> </script>

Only standard and script tags are guaranteed to work on any PHP configuration. Short tags and ASP tags must be explicitly enabled in php.ini.

To enable short tags, ensure that the short_open_tag switch in php.ini is set to on to "On":

short_open_tag = On;

To enable ASP-style tags, ensure that the asp_tags is enabled in php.ini:

asp_tags = On;

Because both short tags and ASP-style tags require changes to the server configuration, it's best to use standard tags to ensure that your code is portable and reusable.

Additionally, <? tells an XML parser to expect a processing instruction and is often included in XML documents. Therefore, if you intend to include XML in your script, you should avoid using short tags because the PHP engine is likely to confuse XML processing instructions and PHP start tags. If you intend to incorporate XML in your document, you should disable short tags.