Short Tags and PHP 6

From Acenet Knowledgebase
Jump to: navigation, search

Within a PHP script, PHP code is enclosed by PHP tags. The opening tag <?php and the closing tag ?> tell the PHP interpretter that everything in between should be identified as PHP code. However, PHP also supports variations of these tags known as "short tags".

The opening short tag <? can be used instead of the long form <?php. Functionally, there's no difference between these two tags, the former simply saves you from typing a few more characters.

PHP also supports a short tag that will indicate the following code should be printed. The short tag <?= is equivalent to typing the long form <?php echo. Again, these two forms perform the same functionality, but the short tag improves readability and saves on typing.

These two examples are functionally equivalent:

<?= "Hello world" ?>
<?php echo "Hello World" ?>

Here at Acenet, short tags are enabled on all of our shared servers by default. However, in new installations of PHP 5.3, short tags are disabled by default in the php.ini. If you administer your own server and wish to enable short tags, open your server's php.ini and set this value:

short_open_tag = On

and then restart your web server.

Short tags can be convenient to type, but it is generally recommended that they NOT be used.

First, short tags are not guaranteed to be enabled on all PHP installations. In production scripts, you want your code to be as portable as possible and this means using the long form of PHP tags which are guaranteed to be supported.

Second, short tags are expected to be deprecated in PHP 6. Using short tags in your code now will require you to rewrite your code when PHP 6 is released in the future. This deprecation is due to the short tag form <? conflicting with XML opening tags. In the interest of respecting other languages' syntax, these short tags will no longer be supported.