Php is commented out in html.

I created a simple html page containing the following php code in html code.

<?php echo date('l, F jS, Y'); ?> 

When I run the html page and I view the source code, it shows:

 <!--?php echo date('l, F jS, Y'); ?--> 

What am I doing wrong? why is it commented?

+8
html php
source share
5 answers

To run PHP scripts, you must save the file as a .php file. You will also need to execute it on the server. You cannot run php directly from your browser, since PHP is an HTML preprocessor - your browser has nothing to do with PHP, it only receives HTML that is generated by the server.

So, since PHP tags are not valid in HTML files, when it is not pre-processed by the server, the browser does not recognize it, so it automatically converts it into comments because it does not know what else to do with it.

+29
source share

What am I doing wrong?

If the file is served by Apache , you have not enabled the PHP interpreter to run in html files. Apache (by default) does not start the php interpreter in html files.

why is he commented?

As others have noted, the browser does not know what to do with the php tag.



If you want to interpret php in html files instead of renaming files in .php, you can add the .html extension to the php interpreter as shown below:

 AddType application/x-httpd-php .php .html 

This line is in the httpd.conf file.

I do not suggest that this is the right way to do this, but I believe that it answers your first question.

+10
source share

I know this is a bit late, but I had the same problem and fixed it by changing the URL from the file: // www / [Project] to localhost / [Project].

Even if the file is saved as .php, it will comment on PHP if the file is opened from the file system.

+4
source share

Link

Create a file called hello.php and place it in the root directory of your web server (DOCUMENT_ROOT) with the following contents:

Example # 1 Our first PHP script: hello.php

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

Use your browser to access the file with the URL of your web server ending in a link to the /hello.php file. When developed locally, this URL will look like http://localhost/hello.php or http://127.0.0.1/hello.php , but it depends on the configuration of the web server. If everything is configured correctly, this file will be analyzed by PHP, and the following output will be sent to your browser:

 <html> <head> <title>PHP Test</title> </head> <body> <p>Hello World</p> </body> </html> 
+3
source share

Since you cannot run PHP directly in the browser, you need to have a server, and at the same time you need to have a .php file to execute the PHP script.

0
source share

All Articles