2

I've just discovered this way to output things on php templates:

<body>
  my name is
  <?= $params['name'] ?>
  ...

That should be equal to:

<body>
  my name is
  <?php echo $params['name'] ?>
  ...

It's a very cool and clean syntax, but my question is: is legit? Or is deprecated?

cl0udw4lk3r
  • 2,663
  • 5
  • 26
  • 46
  • 2
    `=` is a short open tag, and yes it is legitimate to use. Many consider it good practise to use also. – Karl Oct 21 '13 at 08:58
  • 2
    Have a look at http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use - tl;dr: You _can_ use them, but today it is not recommened to use the short open tags and it's not a good practive to mix code from your views with PHP code. – akluth Oct 21 '13 at 09:01
  • It is legit, also check this question, it might give you some more info: http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – Henk Jansen Oct 21 '13 at 09:01
  • As of PHP 5.5 `=` is not related to short open tags, so if they are off, the short echo will still work – Royal Bg Oct 21 '13 at 09:03
  • 1
    It's been possible for many years, but was considered _poor_ practice (cc: @Karl) up until recently. This was because code using short tags was not portable between servers with different configurations - until the PHP team decided to turn on `short_tags` permanently, as Royal says. – halfer Oct 21 '13 at 09:07
  • @akluth You should not mix up arguments when it comes to separating logic. Could you give me just one example how to use the short open tag and not use PHP code? How can this be bad practice? To create a view with PHP, you still have to use PHP code, as in `=`, `foreach`, etc. This has nothing to do with `bad practice`. Every template compiler build on top of the PHP compiler still uses _PHP code_ in the long run. – dbf Oct 21 '13 at 09:47
  • In my opinion this is not a bad practice I'd like to use this shorten notation only for templating where it seems more clean to me than use the normal notation. – cl0udw4lk3r Oct 21 '13 at 09:51

1 Answers1

3

Yes it's legitimate to use, however some times short tags are not enabled.

To turn them on if they are off, you can do this:

  1. go to php.ini file

  2. find, 'short_open_tag' and set it to on,

  3. restart the server

Albzi
  • 15,431
  • 6
  • 46
  • 63