1

I have some php variables that I need to spit out in a template file.

Normally I would do <?php echo $var; ?> But I know you can also do <?=$var?> and it will do the same thing.

I know I've seen a setting for "enable php shorttags" or something like that. Meaning that the server will interpret both <? and <?php

My question is, do shorttags have to be allowed in order to the method of getting vars <?=$var?> ?

Thanks!

Senica Gonzalez
  • 7,996
  • 16
  • 66
  • 108

5 Answers5

5

It depends on the PHP version.

As of 5.4.0, <?= always works. Prior to that, short_open_tag needs to be enabled in PHP.ini.

See also: http://php.net/manual/en/ini.core.php

I'd avoid using them, for maximum portability.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 1
    It should also be noted that PHP 5.4 is only in beta. This essentially means that short_open_tag needs to be on for any practical application. – Levi Morrison Nov 02 '11 at 15:05
0

Yes, short tags is required for <?=. People hate on short tags regularly, under the mantra of "your code won't be portable. the new server might not have them enabled!". So... if you're going for portability, avoid short tags. If you can guarantee the operating environment and can turn them on, then feel free to use them.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

Since PHP 5.4.0, <?= is always available. Before that, short_open_tag have to be allowed to using this.

xdazz
  • 158,678
  • 38
  • 247
  • 274
0

The answer is yes for versions of PHP prior to 5.4.0:

Note: 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.

Matt Montag
  • 7,105
  • 8
  • 41
  • 47
0

From the manual:

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.

Also,

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.

dnagirl
  • 20,196
  • 13
  • 80
  • 123