简体   繁体   中英

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 ;?> .

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. Ensure that you have a webserver running:

If you have accesst to the command line on a Gnu/Linux derivate you can start PHP's internal server by:

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. Then your code runs fine.

In the long run, you want to set up a nginx or an apache2. Another aspect to look into is using vagrant to setup a virtual Linux machine that creates your development enviroment.

You are trying to open the file directly in your web browser, but unlike HTML, your browser is incapable of understanding 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.

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). Installing and running this will easily set up a web server on your computer.

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 . It should teach you the basics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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