What is the difference between the PHP open tags “<?=” and “<?php”/“<?”?
Sorry for the silly question, but I ran across code that used:
<?=$MAP_OBJECT->printOnLoad();?>
<?=$MAP_OBJECT->printMap();?>
<?=$MAP_OBJECT->printSidebar();?>
Is there anything special about <?=
over <?php
or just plain <?
?
Rather than talking about whether short_open_tags is deprecated or not we should talk about the advantages and disadvantages when using short open tags:
Advantages
Using the short open tags <?
along with <?=
is shorter and probably easier to write than the standard opening tags <?php
and <?php echo
respectively. That's quite handy when using PHP directly in a template. (That's probably also the reason why PHP has an alternative syntax for control structures.)
Disadvantages
Requires a specific configuration
When using short open tags you are required to have short_open_tags enabled. If you or your web hosting provider decides to disable short_open_tags, your application probably won't work any more and you can have some serious security issues. Because if short_open_tags is disabled, only the standard opening tags <?php
are recognized and everything inside short opening tags is treated as plain text. (See also the blog post referenced in Sarfraz Ahmed's answer.)
This requirement makes your PHP application less portable if you aim to write applications that are not just for you. That'a also why many recommend not to use short open tags (including the PHP manual):
Note: 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.
As of PHP 5.4 <?=
is always available, regardless of the short_open_tags
option. <?
on the other hand requires the option to be enabled.
Conflicts with XML processing instructions
Another issue is when using XML processing instructions like <?xml … ?>
. When short_open_tags is enabled you cannot use them directly in your code but need to use PHP to output it:
If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?>
inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>
<?php echo '<?xml version="1.0"?>'; ?>
.
Otherwise PHP will choke on the xml
in <?xml
.
Now some last words about the deprecation: Currently short_open_tags is not deprecated. Otherwise the manual would state that explicitly. Additionally, Rasmus Lerdorf, inventor of PHP, wrote in a reply on the question “Is it true that short_open_tag is deprecated in PHP 6?” on the internals mailing list that there were several reasons not to remove short_open_tags in PHP 6:
Which is one of the reasons we decided not to remove them in PHP 6.
They are shorthand of <?php echo .... ?>
known as short tags . You should avoid using them because:
<?xml ... ?>
Therefore you should never use them again, also have a look at:
PHP Short Open Tag: Convenient Shortcut or Short Changing Security?
<?=
is a short tag that is pretty much equivalent to:
<?php echo
As of PHP 5.4, this syntax is always available, but in earlier versions of PHP, it needs short_open_tag
enabled. As for how to search for it on Stack Overflow, try code:"<?="
.