Possible Duplicate:
What is the difference between the PHP open tags “<?=” and “<?php”/“<?”?
Rather than type:
<?php echo $foo; ?>
I have seen it written
<?= $foo; ?>
But I've often wondered what the risk/impracticalities are of doing it? Just curious. Thanks!
If you happen to move the code to an environment where short_open_tag
isn't enabled, you'll be exposing a lot of internal variable names (security issue) and have a whole lot of damaged output.
The other downside is that the same setting that allows usage of <?=
is the same that lets you open PHP tags with just <?
, so having it disabled would not only expose those specific variables you were attempting to display, but also display any PHP code within short tags.
It's not portable. There's a pre-5.4 configuration setting to turn it off, so if you move your script to a site where it is disabled, it would break
Also, it's less explicit in my opinion. The difference between <?= func() ?>
and <? func(); ?>
is easy to miss, but important
The second option increases the readability. The first ensures portability to other systems.
Other than that, there is no difference at all...
You can only use <?
and <?=
if short tags are enabled when you are running PHP. The actual reason not to use it is because it's incompatible with an xml declaration. If you are trying to output xml with a php extension and you have short tags enabled, you have to do something like <<??>?
.. I suppose you can just echo a string.
I don't understand all that portability talk.
There is ALWAYS a portability issue.
There can be no apache - so, don't use mod_rewrite.
There can be no PDO - so, don't use prepared statements.
There can be no mysql - so, don't use complex queries.
There can be no PHP - so, plain HTML is most compatible format, never use anything else because of portability issues!