简单的 PHP 回显代码不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7639271/
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
Simple PHP echo code not working
提问by edorahg
Here is my html with a php script:
这是我的带有 php 脚本的 html:
<html> <head> <title>Bob's Auto Parts</title> </head> <body> <h1>Bob's Auto Parts</h1> <table width = 100% > <tr> <?php echo "<td>This is working.</td>"; ?> </tr> </table> </body> </html>
<html> <head> <title>Bob's Auto Parts</title> </head> <body> <h1>Bob's Auto Parts</h1> <table width = 100% > <tr> <?php echo "<td>This is working.</td>"; ?> </tr> </table> </body> </html>
Why is the output of this appearing with a ; ?>. I want it to be 'This is working.' only. Here is the ouput
为什么这个输出带有 ; ?>。我希望它是“这是有效的”。只要。这是输出
Bob's Auto Parts
Bob's Auto Parts
This is working."; ?>
鲍勃的汽车零件
鲍勃的汽车零件
这是有效的。”; ?>
I know I am doing something wrong here but not able to figure it out. Thanks in advance.
我知道我在这里做错了,但无法弄清楚。提前致谢。
采纳答案by Rene Pot
Any of these (or more) could be your answer why it is not working
这些(或更多)中的任何一个都可能是您为什么它不起作用的答案
- Is there actually PHP running on your computer?
- Is the file extension
.php? - Are you accesing the file through your browser doing something like
http://localhost/myfile.php - If it is on a remote server, is there PHP installed?
- 你的电脑上真的有 PHP 运行吗?
- 是文件扩展名
.php吗? - 您是否通过浏览器访问该文件,例如
http://localhost/myfile.php - 如果它在远程服务器上,是否安装了 PHP?
回答by Kirk
Make sure that you are using <?phpand not <?shorthand since that may be disabled on your server. This will cause the output of "; ?> as it happened to me a few months ago in a transition to PHP5.
确保您正在使用<?php而不是<?速记,因为这可能在您的服务器上被禁用。这将导致 "; ?> 的输出,就像几个月前我在向 PHP5 过渡时发生的那样。
I've only seen the odd output like this when the PHP parser isn't detecting it as PHP. Make sure to check that PHP is functioning as expected and that the <?phptag is being recognized.
当 PHP 解析器未将其检测为 PHP 时,我只看到过这样的奇怪输出。确保检查 PHP 是否按预期运行以及<?php标签是否被识别。
回答by Lucio Mollinedo
In my case (which is a very specific case) installing this missing package (Ubuntu 14.04) did the trick:
在我的情况下(这是一个非常特殊的情况)安装这个丢失的包(Ubuntu 14.04)可以解决问题:
sudo apt-get install libapache2-mod-php5
for users working with php7 install the package:
对于使用 php7 的用户安装包:
sudo apt-get install libapache2-mod-php7.0
After that, just restart apache:
之后,只需重新启动apache:
sudo service apache2 restart
And there you go.
你去吧。
回答by Gunnar Bernstein
I had the same problem when I figured out my mistake:
当我发现我的错误时,我遇到了同样的问题:
Instead of correct http://localhost/test.phpI just double clicked on the file file:///C:/Users/.../htdocs/test.php.
http://localhost/test.php我只是双击文件而不是正确的file:///C:/Users/.../htdocs/test.php。
回答by Zaki
The file was being saved in UniCode Encoding. Opened the file in Notepad, and saved by changing the Encoding to "ANSI" and saved the file as fileName.php and type "All Files", Encoding as "ANSI".
文件以 UniCode 编码保存。在记事本中打开文件,通过将编码更改为“ANSI”保存并将文件保存为 fileName.php 并键入“所有文件”,编码为“ANSI”。
回答by Rahul Soshte
Check if you have installed more than 2 versions of PHP. The server may get confused sometime regarding this. First uninstall the php versions and reinstall only one PHP version.
检查您是否安装了 2 个以上的 PHP 版本。服务器有时可能会对此感到困惑。首先卸载php版本,只重新安装一个PHP版本。
回答by Bala vallivel
http://localhost/demo/demo.htmlwill not work. http://localhost/demo/demo.phpwill work. php work on .php extension. Good Luck bro
http://localhost/demo/demo.html将不起作用。 http://localhost/demo/demo.php将起作用。php 处理 .php 扩展名。祝你好运,兄弟
回答by matthew n
In order to display PHP, or run embededded PHP, you need to tell PHP to look inside the HTM & HTML files.
为了显示 PHP 或运行嵌入式 PHP,您需要告诉 PHP 查看 HTM 和 HTML 文件的内部。
You need to have a file in the root folder of the HTML directory called .htaccess, which is a simple text file, with the following line:
您需要在 HTML 目录的根文件夹中有一个名为 的.htaccess文件,它是一个简单的文本文件,包含以下行:
#AddType application/x-httpd-php .html .htm
#AddType application/x-httpd-php .html .htm
This lets the PHP compiler know to compile the php when displaying the HTML page.
这让 PHP 编译器知道在显示 HTML 页面时编译 php。

