Here's the information according to the official documentation:
There are four different pairs of opening and closing tags which can be used in PHP. Two of those,
<?php ?>
and<script language="php"> </script>
, are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.
In my experience most servers do have short tags enabled. Typing
<?=
is far more convenient than typing
<?php echo
The programmers convenience is an important factor, so why are they not recommended?
Convert
<?
(without a trailing space) to<?php
(with a trailing space):Convert
<?
(with a trailing space) to<?php
(retaining the trailing space):http://uk3.php.net/manual/en/language.basic-syntax.phpmode.php has plenty of advice, including:
and
and
Let's face it. PHP is ugly as hell without short tags.
You can enable them in a
.htaccess
file if you can't get to thephp.ini
:One has to ask what the point of using short tags is.
Quicker to type
MDCore said:
Yes, it is. You save having to type 7 characters * X times throughout your scripts.
However, when a script takes an hour, or 10 hours, or more, to design, develop, and write, how relevant is the few seconds of time not typing those 7 chars here and there for the duration of the script?
Compared to the potential for some core, or all, of you scripts not working if short tags are not turned on, or are on but an update or someone changing the ini file/server config stops them working, other potentials.
The small benefit you gain doesn't comes close to outweighing the severity of the potential problems, that is your site not working, or worse, only parts of it not working and thus a headache to resolve.
Easier to read
This depends on familiarity.
I've always seen and used
<?php echo
. So while<?=
is not hard to read, it's not familiar to me and thus not easier to read.And with front end/back end developer split (as with most companies) would a front end developer working on those templates be more familiar knowing
<?=
is equal to "PHP open tag and echo"?I would say most would be more comfortable with the more logical one. That is, a clear PHP open tag and then what is happening "echo" -
<?php echo
.Risk assessment
Issue = entire site or core scripts fail to work;
The potential of issue is very low + severity of outcome is very high = high risk
Conclusion
You save a few seconds here and there not having to type a few chars, but risk a lot for it, and also likely lose readability as a result.
Front or back end coders familiar with
<?=
are more likely to understand<?php echo
, as they're standard PHP things - standard<?php
open tag and very well known "echo".(Even front end coders should know "echo" or they simply wont be working on any code served by a framework).
Whereas the reverse is not as likely, someone is not likely to logically deduce that the equals sign on a PHP short tag is "echo".
Because the confusion it can generate with XML declarations. Many people agree with you, though.
An additional concern is the pain it'd generate to code everything with short tags only to find out at the end that the final hosting server has them turned off...
I'm too fond of
<?=$whatever?>
to let it go. Never had a problem with it. I'll wait until it bites me in the ass. In all seriousness, 85% of (my) clients have access to php.ini in the rare occasion they are turned off. The other 15% use mainstream hosting providers, and virtually all of them have them enabled. I love 'em.