0

I sort of discovered by accident that

<?php ... ?>

can be shortened to

<? ... ?>

Is this a bad idea? In some cases? What cases are they? Are there anymore shorthand examples? I am aware of shorthand examples involving conditional statements but I don't find them easier to read.

Please link me if this has been answered elsewhere, but I could not find it via a search. Maybe I'm not using the right keywords.

Daniel Schwarz
  • 284
  • 1
  • 10
  • IMHO avoid them as this will confuse folks with XML – Ed Heal Apr 18 '14 at 16:33
  • 2
    I've never understood the use of short tags; the additional three keystrokes aren't really *that* much of a problem; and using short tags seems to risk unnecessary problems. – David Thomas Apr 18 '14 at 16:34
  • 1
    I concur with @DavidThomas. They are the hallmark of the lazy, in my experience. I rather _like_ that a PHP block starts with the text `php`. It's hard to misunderstand your code when it's so clear. – Lightness Races in Orbit Apr 18 '14 at 16:38

4 Answers4

2

This shorthand has been available for a very long time, but its use is discouraged (and nowadays disabled by default) because of various incompatibilities with other languages — ambiguity with ASP's ability to accept <? x ?>, and with various XML constructions, are two obvious examples.

Opt not to use it.

By contrast, the <?= x ?> shorthand (equivalent to <?php echo x ?>) has had a resurgence in popularity and is enabled by default since PHP 5.4, because it does not suffer from the same problems.

As always, consult the documentation for canonical information on such things.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

The shorthand tags are only enabled in certain setups. They are discouraged. See the PHP manual entry for the tags: http://www.php.net/manual/en/language.basic-syntax.phptags.php

PHP also allows for short open tags (which are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option.

As far as other shorthands, no others exist. The <?= ?> shorthand could possibly be considered one, though it's only usage is to output a variable (as mentioned in another answer to this question). The omission of the closing ?> is sort-of one. It's handy for documents that contain nothing beyond PHP code. This technique is also mentioned in the manual entry I linked above.

blakeo_x
  • 495
  • 5
  • 14
0

Will it work? In some cases. PHP has a few opening tags available, take a look here. But you should be careful as it depends on the php configuration (usually /etc/php.ini or /etc/php/php.ini). There is an option short_open_tag = On. if it's not enabled, the code will be rendered in the view and won't be executed as php code.

Alexander Ejbekov
  • 5,594
  • 1
  • 26
  • 26
0

As blakeo_x said, the shorthand is only valid if the PHP configuration file has it enabled. It is discouraged because you might have to run it on a different server where shorthand is not enabled. Hence try to stick to the longer version <?php instead.

Patrick Geyer
  • 1,515
  • 13
  • 30