简体   繁体   English

为什么php echo打印出?>消息?

[英]Why does php echo print out ?> message?

With the following code below, 使用下面的代码,

<html>
    <head>
        <title>PHP Test</title>
    </head>
    <body>
        <?php echo '<p>Hello World</p>'; ?>
    </body>
</html>

I want only the Hello World statement to be printed, but it also prints out ;?> . 我只希望打印Hello World语句,但也可以打印出;?>

After I inspected the element, it came out like this. 在检查了元素之后,它就这样出来了。

 <body>
    <!--?php echo '<p-->
    Hello World
    <p></p>
    "; ?>"
 </body>

Your website is treated as plain HTML. 您的网站被视为纯HTML。 Ensure that you have a webserver running: 确保您正在运行Web服务器:

If you have accesst to the command line on a Gnu/Linux derivate you can start PHP's internal server by: 如果您可以访问Gnu / Linux派生版本上的命令行,则可以通过以下方式启动PHP的内部服务器

php -S 127.0.0.1:1337

This command has to be started in your web root and you should have an index.php living there. 该命令必须在您的Web根目录中启动,并且您应该在那里拥有一个index.php Then your code runs fine. 然后您的代码运行良好。

In the long run, you want to set up a nginx or an apache2. 从长远来看,您想设置一个nginx或apache2。 Another aspect to look into is using vagrant to setup a virtual Linux machine that creates your development enviroment. 要研究的另一个方面是使用vagrant设置可创建您的开发环境的虚拟Linux计算机。

You are trying to open the file directly in your web browser, but unlike HTML, your browser is incapable of understanding PHP. 您试图直接在Web浏览器中打开文件,但与HTML不同,您的浏览器无法理解PHP。

PHP needs to be run server-side behind Apache, Nginx or any viable web server, your web browser will then request pages from that webserver, which will request PHP to serve your browser the fully generated page. PHP必须在Apache,Nginx或任何可行的Web服务器之后的服务器端运行,然后您的Web浏览器将从该Web服务器请求页面,这将请求PHP为您的浏览器提供完整生成的页面。

Practically, if you're trying PHP for the first time, you want to install a development LAMP stack such as WampServer (for Windows only), XAMPP (for Linux, Windows, OS X) or MAMP (for OS X, Windows). 实际上,如果您是第一次尝试使用PHP,则需要安装开发LAMP堆栈,例如WampServer (仅适用于Windows), XAMPP (适用于Linux,Windows,OS X)或MAMP (适用于OS X,Windows)。 Installing and running this will easily set up a web server on your computer. 安装并运行它可以轻松在您的计算机上设置Web服务器。

I will not write a full tutorial on how to use a LAMP stack but I can recommend you to read that article from Kasia Mikoluk on Udemy . 我不会编写有关如何使用LAMP堆栈的完整教程,但是我建议您阅读Udemy上Kasia Mikoluk的文章 It should teach you the basics. 它应该教你基础知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM