Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is Short tag good practice in php? [duplicate]

Tags:

php

if I enable short tag = true, then I can use like below

<?=$variableName ?>

Instead of

<?php echo $variableName ?>

I have below questions:

  1. Does it good practice?
  2. Is there any server dependency?
  3. All open sources and framework support for this?
like image 322
Butterfly Avatar asked Dec 24 '22 20:12

Butterfly


2 Answers

Short tags <? doSomething(); ?> are considered to be a bad practice because they are not XML compliant... whether you care about that or not is another issue.

Short echos <?= $myString ?> are not a bad practice, it's just not the best. PHP is a templating engine, however much better engines are available (Twig, Mustache, Smarty, etc). Most frameworks include their own templating engine so short tags don't need to be used.

Up to and including PHP 5.3, these types of tags were considered to be the same thing. Since PHP 5.4 however they've been separated out and short echo is allowed without enable-short-tags being turned on. Since PHP 5.3 is no longer supported, the only concern is if you're being forced to use an unsupported version, which obviously has it's own implications. :)

like image 183
DanielM Avatar answered Dec 27 '22 09:12

DanielM


  1. It is not bad practice, per se. New versions of PHP have it enabled, but older versions may or may not have it enabled. So, if you want everyone to be able to run your code then you should go with <?php. If you do not care about older versions of PHP (i.e. no backwards compatibility concerns) then... do as you like.

  2. No server dependencies. You just need to enable it.

  3. Frameworks generally have guidelines for contributors. Which one you use personally has nothing to do with the framework, however; it has to do with if it is enabled or not.

like image 39
Sverri M. Olsen Avatar answered Dec 27 '22 10:12

Sverri M. Olsen