且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是短标签在PHP中的好习惯?

更新时间:2023-12-04 13:36:22

短标签<? doSomething(); ?>被认为是不好的做法,因为它们不符合XML规范……不管您是否在乎,这是另一个问题./p>

短回声<?= $myString ?>并不是一个坏习惯,但这并不是***的做法. PHP是一个模板引擎,但是可以使用 更好的引擎( Twig 胡子聪明等).大多数框架都包含自己的模板引擎,因此不需要使用短标签.

在PHP 5.3之前(包括PHP 5.3),这些类型的标签被认为是同一回事.但是从PHP 5.4开始,它们已经被分离出来,并且允许在不打开enable-short-tags的情况下进行简短的回显.由于不再支持PHP 5.3,因此唯一需要考虑的是是否***使用不受支持的版本,这显然具有其自身的含义. :)

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?

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