PHP echo vs PHP short echo tags

87,745

Solution 1

<? and <?= are called short open tags, and are not always enabled (see the short_open_tag directive) with PHP 5.3 or below (but since PHP 5.4.0, <?= is always available).

Actually, in the php.ini-production file provided with PHP 5.3.0, they are disabled by default:

$ grep 'short_open' php.ini-production
; short_open_tag
short_open_tag = Off

So, using them in an application you want to distribute might not be a good idea: your application will not work if they are not enabled.

<?php, on the other side, cannot be disabled -- so, it's safest to use this one, even if it is longer to write.


Except the fact that short open tags are not necessarily enabled, I don't think there is much of a difference.

Solution 2

Echo is generally just better to use because...

  1. It supports good programming style.
  2. It can't be turned off in php.ini (short tags can be)
  3. Short tags will be removed in PHP 6)

But, they are generally the same. See also:

  1. Are PHP short tags acceptable to use?
  2. How are echo and print different in PHP?

Solution 3

http://php.net/manual/en/language.basic-syntax.phpmode.php states:

Starting with PHP 5.4, short echo tag is always recognized and valid, regardless of the short_open_tag setting.

short_open_tag Off or On doesn't matter anymore.

So now you can, without concern, put tags like this in your templates:

    <?= (($test) ? "val1" : "val2") ?>

It is official now, the "short echo tag" is something very different than the "short tag".

Solution 4

Apart from the whole semi-religious debate on whether or not using short tags are a good idea and whether or not it should be considered deprecated, the original question was on how safe or unsafe they are to use.

Simply put, if you use short tags on a server that doesn't support them, parts of your PHP code may be exposed which can be considered a security vulnerability.

Solution 5

Just to add another source of PSR: http://www.php-fig.org/psr/psr-1/

PHP code MUST use the long tags or the short-echo tags; it MUST NOT use the other tag variations.

specifying:

 <?php ?> and <?= ?>
Share:
87,745
homework
Author by

homework

Updated on July 18, 2020

Comments

  • homework
    homework almost 4 years

    Are they equal in safeness? I was informed that using

    <?=$function_here?>
    

    was less safe, and that it slows down page load times. I am strictly biased to using echo.

    What are the advantages/disadvantages?

  • mpen
    mpen over 14 years
    what?! 6 is removing them? lame-sauce! if i'm in charge of the server i don't care about portability. <?= is so much nicer than <?php echo
  • mauris
    mauris over 14 years
    @Mark - it's more standardized for all to use <?php ?>, instead of one programmer using <% %>, the other using <%= %>, and another one <? ?> and they don't know they all speak the same language.
  • mauris
    mauris over 14 years
    Worse of all: <script language="php"> ... </script>
  • DGM
    DGM over 14 years
    The php community's aversion to short tags just baffles me; removing them altogether n php6 is just final proof to me that the php design team are nuts. <?= $foo ?> is so much cleaner in a template than <?php echo $foo; ?>
  • Your Common Sense
    Your Common Sense about 14 years
    php6 reason is just not true
  • keyle
    keyle almost 14 years
    yeah apparently php6 will lose the asp notation: "<%". Not all short-tags. So "<?" is still valid and reason "3" above is false.
  • Gordon
    Gordon over 13 years
    PHP6 or whatever the next version will be called will NOT remove short-open-tags. If anything they will be disabled by default.
  • Slawek
    Slawek over 12 years
    Maybe using GD or CURL or PHP at all is not a good idea too, because they're external modules and can be turned off on some lame shared hosting providers? It's complete BS they're almost always turned ON.
  • Pascal MARTIN
    Pascal MARTIN over 12 years
    Considering they are turned off in the default production configuration, I think they'll be turned on less and less often ;; considering that not using them is simple/easy... well, you are of course free of using whatever syntax you want, I just hope for you that you will not have to deploy your application on a server that has the default configuration for this directive ;-)
  • Slawek
    Slawek over 12 years
    Default configuration for PHP, mysql and apache isn't suitable for production usage and we all know it :) You're only advocating writing unreadable code... because of what? One server out of 40 may not support short tags? :) ... Beside This directive also affected the shorthand <?= before PHP 5.4.0, which is identical to <? echo. Use of this shortcut required short_open_tag to be on. Since PHP 5.4.0, <?= is always available.
  • Pascal MARTIN
    Pascal MARTIN over 12 years
    I unfortunately don't have numbers to back up my theory, but I wouldn't take the 1/40 chance on this point (and, in fact, I am not -- and I work in a company in which we configure our servers ourselves, and do not depend on an external hosting service) ;; I should add that PHP 5.4 is not released (yet -- it should be in a few days/weeks), and it will takes months to be installed on 1/40 servers out there (not to mention the time it'll take to have it installed on 50% of them... )
  • Blouarf
    Blouarf about 9 years
    short echo tag CANNOT be turned off in the php.ini. And it was supported by Rasmus himself.
  • Sean the Bean
    Sean the Bean almost 7 years
    Thank you @keyle for that clarification! :)
  • Scott C Wilson
    Scott C Wilson almost 2 years
    This is the correct modern answer. Some of the older answers are confusing short tags (which can be turned off and are deprecated) with short echo tags (which cannot be turned off).