is Short tag good practice in php?

前端 未结 4 509
长发绾君心
长发绾君心 2020-12-11 19:56

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


Instead of



        
相关标签:
4条回答
  • 2020-12-11 20:38

    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.

    0 讨论(0)
  • 2020-12-11 20:48
    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.

    0 讨论(0)
  • 2020-12-11 20:53
    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.

    0 讨论(0)
  • 2020-12-11 21:00

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

    0 讨论(0)
提交回复
热议问题