PHP - 进行内联 HTML 变量输出的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2695715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - what is the proper way to do inline HTML variable output?
提问by Peter Tirrell
I just moved from an Ubuntu PHP workstation dev environment back to Windows and am using xampp. I have a bit of code, like so:
我刚刚从 Ubuntu PHP 工作站开发环境移回 Windows 并使用 xampp。我有一些代码,如下所示:
<input type="text" name="txtEmail" value="<?=$emailaddress;?>"/>
that I swearworked to display the variable in the textbox when I was developing before. But when I loaded the page on Windows/xampp it just put that text between the quotes in the textbox. Instead, I ended up changing it to something like:
我发誓在我之前开发时在文本框中显示变量。但是当我在 Windows/xampp 上加载页面时,它只是将该文本放在文本框中的引号之间。相反,我最终将其更改为以下内容:
<input type="text" name="txtFirstName" value="<?php echo($firstname);?>" />
The latter makes sense, but I guess I thought there was a shorthand or something, and I must be going crazy because I'm sure the first way was working on a difference environment.
后者是有道理的,但我想我认为有速记之类的,我一定要疯了,因为我确定第一种方法是在不同的环境中工作。
So what's the best way to do this?
那么最好的方法是什么?
回答by Syntax Error
Save yourself a headache and don't use short tags. They feel dirty, they don't work on every setup, and I thinkthey may be getting phased out of php altogether (That might be wrong though).
不要让自己头疼,不要使用短标签。他们觉得很脏,他们不能在每个设置上工作,而且我认为他们可能会完全淘汰 php(虽然这可能是错误的)。
Even if you know how to fix them, you'll still be irritated every time you have to change your new server to make them work.
即使您知道如何修复它们,每次必须更改新服务器以使其正常工作时,您仍然会感到恼火。
Edit:Yup, they're getting depracated. http://cubicspot.blogspot.com/2009/06/maximum-failure-php-6-deprecates-short.html
编辑:是的,他们被贬低了。http://cubicspot.blogspot.com/2009/06/maximum-failure-php-6-deprecates-short.html
2017 Edit:To add an important distinction - the <?="short echo" format is always enabled (see Dav's comment below). The <?"short open" format is a separate matter. For more info see Why are "echo" short tags permanently enabled as of PHP 5.4?
2017 年编辑:添加一个重要的区别 -<?=始终启用“短回声”格式(请参阅下面的 Dav 评论)。该<?“短开放”的格式是另外一个问题。有关更多信息,请参阅为什么自 PHP 5.4 起永久启用“echo”短标签?
回答by Michael Mrozek
There's a php.inidirective that controls it. Make sure yours is set to:
有一个php.ini指令来控制它。确保您的设置为:
short_open_tag = On
回答by Adam Gotterer
When doing inline commands the semi-colon isn't required. I personally prefer the short tags, typing <?php echogets old after a while.
执行内联命令时,不需要分号。我个人更喜欢短标签,<?php echo一段时间后打字会变老。
回答by mario
Yeah, PHP developers are deprecating short tags, because it mightconflict with XML. Quite in time for XHTML getting tossed out in favour of a SGML based HTML5. And nobody was using PHP to output XML anyway..
是的,PHP 开发人员正在弃用短标签,因为它可能与 XML 冲突。XHTML 很快就被基于 SGML 的 HTML5 抛弃了。反正没有人使用 PHP 输出 XML..

