33

I created a simple HTML webpage that includes the following PHP code in the 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 being commented out?

The HTML webpage file has the extension .html.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Renier
  • 1,738
  • 3
  • 24
  • 51

9 Answers9

52

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

So because PHP tags are not valid in HTML files, when not preprocessed by the server, the browser doesn't recognise it, so it automatically converts it to comments since it doesn't know what else to do with it.

Edit for additional information:

If you want to see the result of the processed php file, you'll need to run it from some kind of server (using Apache through XAMPP for example, but there's loads of options). If you then view the resulting page on the localhost server, it'll give you the processed php code, which should be the desired output. You can check the manual for whichever program you're using to run your server for more details on how to do this.

Note that even if you have a server running, opening the .php file locally in your browser still doesn't show you the processed result (ie. it will still show your php code as comments). You need to view the page through something like http://localhost/mypage.php, or whichever location is set as default url for your specific localhost server.

Joeytje50
  • 18,636
  • 15
  • 63
  • 95
  • So the browser is lying. What browser would do that? Internet Explorer? – Peter Mortensen Nov 30 '19 at 15:30
  • In fact, at the time of writing this post, I believe every browser showed this behaviour. Currently, Chrome/FF instead download any .php file not served by a server, but the behaviour can still be seen in these browsers by saving any .php file as .html and then opening it in your browser. It's actually pretty reasonable to do this, because you don't want the `` tags to have any other behaviour. Also HTML has no errors, simply fallbacks. Undefined behaviour has to at least do something, and converting it into comments is a pretty sensible thing to do. – Joeytje50 Dec 01 '19 at 21:54
17

What am I doing wrong?

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

why is it being commented out?

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



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

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

This line is in the httpd.conf file.

I'm not suggesting this is a proper way to do it but I believe it does answer your first question.

CramerTV
  • 1,376
  • 1
  • 16
  • 29
8

In my case it was because I had a syntax error with my php code. I typed

<? php
    phpinfo();
?>

Instead of

<?php
    phpinfo();
?>

The space between <? and php caused the headache for me

crazy_abdul
  • 503
  • 6
  • 12
  • 1
    Interestingly enough, none of the other upvoted solutions worked for me (I didn't even expect them to...) because only one of the tags in the file was causing trouble. I knew something was going on with that particular piece of code, and of course it was some weird space issue between ` – Fr0zenFyr Oct 25 '18 at 04:54
  • 1
    OMG, this needs to go up. I never realized that space is compulsory. It even doesn't throw any error. And I was trying for configuration options etc. – garg10may Aug 01 '19 at 06:37
  • 1
    The OP shows his code and there is no space. This is an answer to a different question – Jon Nov 14 '19 at 16:34
  • 1
    @garg10may: What space is compulsory? The space in this answer causes an error, not the other way around. – Peter Mortensen Nov 30 '19 at 15:28
  • 1
    Upvoted since this is the top result in Google, and even though this may not directly address OP's problem, this surely will help out some people just starting out with PHP and scratching their head why the top answers won't help them. – Joeytje50 Apr 23 '20 at 13:00
5

I had the same problem and fixed it by changing the URL from file://www/[Project] to localhost/[Project].

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2072826
  • 528
  • 1
  • 5
  • 12
4

Reference

Create a file named hello.php and put it in your web server's root directory (DOCUMENT_ROOT) with the following content:

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 your web server's URL, ending with the /hello.php file reference. When developing locally this URL will be something like http://localhost/hello.php or http://127.0.0.1/hello.php but this depends on the web server's configuration. If everything is configured correctly, this file will be parsed 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>
Kaushik
  • 2,072
  • 1
  • 23
  • 31
1

The issue for me not being able to execute php with ampps was I opened the php file directly rather than through localhost

So the url in the browser was file:///ampps/www/example.php when it should have been 127.0.0.1/example.php

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
D M
  • 11
  • 1
0

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

Vivek Pandey
  • 396
  • 5
  • 10
0

I've had similar issues with one of my projects. The html file has PHP includes and PHP codes which were not working when I set up the project on a fresh Cpanel. Then I was wondering why it's working there but not here. Then I figured out that your Cpanel needs to do an additional setup in order to run PHP codes inside html/htm files.

You need to go to apache handlers from your Cpanel dashboard and then add handlers so it handles html/htm files.

This is how it looks like

This is how you do it.

NOTE: Maybe you need to update the handler based on your current default PHP version, I suggest trying the same setting first if it doesn't work then check your default PHP version and rename handler name with php version.

Code Cooker
  • 881
  • 14
  • 19
-1

Another possibility: I got php commented out because the file was saved as Unicode, not ANSI...