4

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?
Anto S
  • 2,448
  • 6
  • 32
  • 50
  • All server and framework support it by defualt – Sunil Pachlangia Apr 15 '15 at 13:29
  • It's fine to use the short tag for that purpose. I don't believe there is a server dependency. I am unsure of the PHP version required though anything recent will do fine. – Frank Apr 15 '15 at 13:29
  • This might answer your question: http://programmers.stackexchange.com/q/151661 – Rizier123 Apr 15 '15 at 13:34
  • 1
    @SunilPachlangia - only if your server is running a modern, supported version of PHP (>=5.4). For many reasons, this isn't always the case. – DanielM Apr 15 '15 at 13:54

4 Answers4

6

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

DanielM
  • 6,380
  • 2
  • 38
  • 57
3
  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.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
1
  1. It not a good practice. Why i said like that because let say you want to push your code to production and you are using shared hosting. In PHP 5.4.0 above php short tags enabled by default. What if the shared hosting use the older PHP version. Some shared hosting you cannot override php.ini settings. If you want to use php shorttags you must enable it on php.ini. Refer the link below. The PHP short tag also can conflict with XML code .

    http://php.net/manual/en/ini.core.php#ini.short-open-tag

  2. No server dependency

  3. It totally not depend on framework.

Faiz
  • 1,021
  • 9
  • 10
  • If you're writing code for PHP 5.4 and then run it on 5.3 you're going to have a whole bunch of other problems. It doesn't make using the feature a bad practice. – DanielM Apr 15 '15 at 14:18
0

To be 100% sure that a variable will be correctly printed out on all servers where your code might possibly run, use <?php echo $variableName; ?>. I personally always use this method.

However, if you're working on a personal project or on a simple project where you know that you will have access to php.ini or where you're not planning to move to another server, then you can safely use it.

Additionaly, <?= is allowed by default on PHP 5.4 and above, which means all currenttly support versions of PHP support this. From php.net regarding the short_open_tag:

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.

So, in short, answer to your three questions:

  1. Is it good practice? No
  2. Is there any server dependency? Yes (need for enabling it on server)
  3. All open sources and framework support for this? It is not possible to speak for all. Another good reason to use the full one

But as mentioned above, if you'll be running the code only on servers with supported versions of PHP, then you can safely use it.

leopik
  • 2,323
  • 2
  • 17
  • 29
  • I have to say I feel this answer is incorrect on each point for any 'supported' version of PHP. At the time of the question, this is PHP 5.4 and above. If the author, as would appear to be the case, is using an unsupported version this should be addressed separately. – DanielM Apr 22 '15 at 14:41
  • Yes, I agree that it is good to mention that all supported versions as of today support `=` and therefore it is safe to use it in general - I have added it to my answe. However, I still think it is not a good practice. According to these statistics (http://blog.pascal-martin.fr/post/php-versions-stats-2014-10-en.html) there is still quite a lot of servers with PHP 5.3 or below (44.26% for PHP 5.3 and 22.75% for PHP 5.2). The statistics is from October 2014, but I think it is still valid and I'd guess the numbers for the two combined have not fallen below an amount that could be neglected. – leopik Apr 22 '15 at 15:27
  • What people currently have on their servers doesn't really have any bearing on how to answer this question. That 73% of people are doing it wrong, doesn't mean we should recommend doing it that way as a best practice... but perhaps that's just me. – DanielM Apr 22 '15 at 16:34
  • Technically, it is completely irrelevant whether OP will use `=` or not. Both are equally correct in current versions. But as you said, 73% of people are doing it wrong and this has to be considered. It is still quite possible that his app will end up on a server that has short tag set to false and his app will not work as expected. He can blame the server admin for not updating the PHP (and he will totally be right - the older versions are obsolete), but in the end of the day, his app will not run and it will be OP's problem. Therefore it is not a good practice to use `=`. – leopik Apr 22 '15 at 16:53
  • That's a much better answer. You should add that to the reason why. ;) – DanielM Apr 23 '15 at 10:50