34

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.

12
  • what do you get you run the code.? Also is your page with .html extension or .php.? Commented Jan 22, 2014 at 10:13
  • what software are u using to run it? Commented Jan 22, 2014 at 10:14
  • 1
    Try to save it as .php Commented Jan 22, 2014 at 10:15
  • 1
    The file is saved as html Commented Jan 22, 2014 at 10:16
  • 2
    When it's .html save it as .php Commented Jan 22, 2014 at 10:16

9 Answers 9

54

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So the browser is lying. What browser would do that? Internet Explorer?
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 <?php ?> 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.
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.

1 Comment

that didn't work at all for me. I'm using XAMPP and I added that line to apache in there.
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

5 Comments

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 <?php and echo ... that appeared like a normal space in my code editor (ST3). Once I re-wrote the line, it was fixed.
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.
The OP shows his code and there is no space. This is an answer to a different question
@garg10may: What space is compulsory? The space in this answer causes an error, not the other way around.
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.
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.

2 Comments

This is not being served on an apache server. If you dont have a php interpreter, then yes you will have all php commented out.
Thank you. This fixed it for me.
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>

4 Comments

Why is this relevant to his problem?
@Loko He need the basic concepts of the php. in that I'm helping him.
@kk12391 Could have added some more info. + I commented when you just posted the code without any good information.
@Loko I was doing some edit to answer to enhance that. Sorry if I'll be late.
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

Comments

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.

Comments

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.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.