<?= 和 <?在 PHP 中回显

<?= and <? echo in PHP

我正在使用 PHP 框架,例如 codeigniter 和 fuelphp,我阅读了一些我推荐 <?= 而不是 <?php echo 的手册。但我没有同时使用它们,而是使用 <? echo,但我仍然不确定我是否做对了。我不习惯使用 <?=。需要经验丰富的 PHP 开发人员的建议。提前致谢。

PHP 已弃用 <? 因为它已被其他编程语言采用

如果你觉得舒服,建议使用<?php

享受你的编码...

首先,<? ... ?><?php ... ?> 的短代码...这是 PHP 文档本身的内容:

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

short_open_tag boolean

Tells PHP whether the short form () of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use inline. Otherwise, you can print it with PHP, for example: '; ?>. Also, if disabled, you must use the long form of the PHP open tag ().

要在您的服务器上运行,必须在您的 php.ini 中启用 short_open_tag。没有正确或错误的写法,但有些人更喜欢旧的长写法<?php ... ?>,比如我,因为我觉得这样更容易阅读。

<?= 也是如此,这是 PHP 文档所说的

echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.

有关回声的更多信息:

http://php.net/manual/en/function.echo.php

这没有对错之分,这是一个基于意见的问题。我个人确实喜欢尽可能使用短标签,因为我发现它更容易阅读。其他人可能会发现

 <?php

更容易阅读,我会更喜欢这个。这是一个完全基于意见的问题,我不会担心。使用你喜欢的。