“为什么”脚本不打印任何内容? [英] 'Why' doesn't the script print anything?

查看:92
本文介绍了“为什么”脚本不打印任何内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下脚本时,什么都不会打印。为什么会这样呢?

When I run the following script, nothing gets printed. Why is it so ?

<?php
  $var = "<?php echo 'Hey !'; ?>";
  echo $var;
?>


推荐答案

它回显无,因为您的浏览器没有了解<?php 标签,因此不会显示标签内容;

It echoes "nothing" because your browser doesn't understand <?php tags, so it won't show the tag contents; it should show something when you select view the page source though.

此行为的原因是脚本的默认内容类型设置为 text / html (您可以通过查看响应标题来确认),并且在HTML上下文中,应使用 htmlspecialchars()

The reason for this behaviour is that the default content type of your script is set as text/html (you can confirm this by looking at the response headers) and in the context of HTML, you should use htmlspecialchars()

echo htmlspecialchars($var);

实际上,作为一般规则,在输出变量时应始终对其进行适当的转义。

In fact, as a general rule, you should always escape variables appropriately when you output them.

或者,您可以让浏览器知道您的输出不应被解释为HTML;您可以通过设置适当的响应标题来做到这一点:

Alternatively you could let the browser know that your output should not be interpreted as HTML; you can do this by setting an appropriate response header:

header('Content-Type: text/plain');

使用上述内容类型,浏览器会逐字显示输出。

With the above content type your output is shown verbatim by the browser.

这篇关于“为什么”脚本不打印任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆