除了懒惰之外,还有一个很好的理由不使用 = 来回显 PHP 变量吗?
可能的重复:
PHP 开放标签之间有什么区别“
而不是输入:
<?php echo $foo; ?>
我已经看到它写了
<?= $foo; ?>
但我经常想知道这样做的风险/不切实际是什么?只是好奇。谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您碰巧将代码移至未启用
short_open_tag
的环境,您将暴露大量内部变量名称(安全问题)并产生大量损坏的输出。另一个缺点是,允许使用
的设置与允许您仅使用
打开 PHP 标签的设置相同,因此禁用它会不仅公开您尝试显示的那些特定变量,还显示短标签内的任何 PHP 代码。
If you happen to move the code to an environment where
short_open_tag
isn't enabled, you'll be exposing a lot of internal variable names (security issue) and have a whole lot of damaged output.The other downside is that the same setting that allows usage of
<?=
is the same that lets you open PHP tags with just<?
, so having it disabled would not only expose those specific variables you were attempting to display, but also display any PHP code within short tags.第二个选项增加了可读性。第一个确保到其他系统的可移植性。
除此之外,根本没有什么区别……
The second option increases the readability. The first ensures portability to other systems.
Other than that, there is no difference at all...
它不便携。有一个 5.4 之前的配置设置可以将其关闭,因此,如果您将脚本移动到禁用它的站点,它就会中断。
另外,在我看来,它不太明确。
和
很容易被忽略,但很重要
It's not portable. There's a pre-5.4 configuration setting to turn it off, so if you move your script to a site where it is disabled, it would break
Also, it's less explicit in my opinion. The difference between
<?= func() ?>
and<? func(); ?>
is easy to miss, but important如果在运行 PHP 时启用了短标记,则只能使用
和
。不使用它的实际原因是它与 xml 声明不兼容。如果您尝试使用 php 扩展输出 xml 并且启用了短标签,则必须执行类似
<?
的操作。我想您可以只回显一个字符串。You can only use
<?
and<?=
if short tags are enabled when you are running PHP. The actual reason not to use it is because it's incompatible with an xml declaration. If you are trying to output xml with a php extension and you have short tags enabled, you have to do something like<<??>?
.. I suppose you can just echo a string.我不明白所有关于可移植性的言论。
始终存在可移植性问题。
不可能有 apache - 所以,不要使用 mod_rewrite。
不能有 PDO - 所以,不要使用准备好的语句。
不可能有 mysql - 所以,不要使用复杂的查询。
不可能有 PHP - 所以,纯 HTML 是最兼容的格式,因为可移植性问题,永远不要使用其他任何格式!
I don't understand all that portability talk.
There is ALWAYS a portability issue.
There can be no apache - so, don't use mod_rewrite.
There can be no PDO - so, don't use prepared statements.
There can be no mysql - so, don't use complex queries.
There can be no PHP - so, plain HTML is most compatible format, never use anything else because of portability issues!