Why are my php tags converted to html comments?
Solution 1
The place to correctly configure PHP operation is the httpd.conf
file, which resides in the conf
subdirectory of your Apache installation directory.
In there, you'll want to look for the module loading section, which will be a bunch of lines that start with LoadModule
. Somewhere in there, you should have the following (or something very similar):
LoadModule php5_module "location\of\your\php\installation"
AddType application/x-httpd-php .php
PHPIniDir "location\of\your\php\configuration\file"
I'm not all too familiar with Linux, but in Windows (WAMP) installations, those would be something along the lines of:
LoadModule php5_module "c:/program files/php/php5apache2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/program files/php"
And the httpd.conf
file, on my machine, is at C:\Program Files\Apache Group\Apache2\conf\httpd.conf
.
It could also be that PHP is simply not installed at all on your machine, in which case, you will have to download it and install it. Brad's already posted the relevant link in one of his comments, (+1, by the way, Brad), but for the sake of having everything in one spot:
PHP: Installation and Configuration - Manual
Solution 2
Your Chrome is lying to you.
Your PHP source file is <?php echo 'test'; ?>
. Because PHP is not executed, this file is sent to the browser. If the browser should interpret this text, it will stumble upon the <?
?>
marks. They have a meaning - they are "XML processing instructions", and the text after the opening angle defines the target.
Obviously the browser does not know about a target named "PHP", so this text is ignored.
And then the element inspector tries to display the DOM and is lying about the original source code, because he is working on the PARSED source - which is great because you usually want to know on which data the browser acts, and this includes how the browser interpreted your source.
But if you make any error, the browser will try to fix it, and the fix is included in the element inspector.
Obviously the fix for an unknown XML processing instruction is to disable it by commenting it out.
Solution 3
This just happened to me. Turned out I had forgotten to change the filetype from .html to .php
Solution 4
This answer doesn't apply to the OP's specific variant of this problem, but I had the basic same issue – <? var_dump($test); ?>
being converted to <!--? var_dump($test); ?-->
– which I could solve by enabling short_open_tag
in php.ini.
Solution 5
Sounds to me that your PHP is not correctly configured or installed in your lamp configuration. What distribution are you using? It might be as simple as running a command to re-install PHP, otherwise you will likely need to compile apache with php support.
Zach Lysobey
Updated on June 17, 2022Comments
-
Zach Lysobey almost 2 years
A friend's lamp host seems to be misconfigured. I try to execute php, but it doesn't seem to be working.
In Chrome's inspect element:
<?php echo 'test'; ?>
becomes :
<!--?php echo 'test'; ?-->
Furthermore, its been triggering a file download, rather than opening it as a webpage.
I've tried various code in an
.htaccess
file, but it doesn't seem to have any effect:AddType x-mapp-php5 .php AddType application/x-httpd-php .php AddHandler x-mapp-php5 .php
-
Matthew over 11 yearsWhy would you be able to inspect
<?php echo 'test'; ?>
in chrome to begin with (if php was running that is)? -
Brad over 11 yearsIt's hard to help you without seeing the relevant parts of your config. Where are you loading the PHP module, or executing it via CGI? php.net/manual/en/install.php
-
kalpaitch over 11 yearswell why would you be forcing php to read .php files, if it doesn't do that already its probably not installed. Last type I added and AddHandler for php that the server didn't understand it downloaded the file, that would be the reason for that.
-
Brad over 11 years@Matthew, The whole point of his question is that PHP isn't running.
-
Sikshya Maharjan over 11 yearsWell, for what it's worth I can confirm that this happens (if php is sent to Chromium, for whatever reason). Though, honestly, why is PHP not running your scripts?
-
Brad over 11 years@DavidThomas, You're only confirming that the PHP start/end tags are attempted to be interpreted by the browser. That has nothing to do with this problem, which entirely server side.
-
Sikshya Maharjan over 11 years@Brad, hence my parentheses...?
-
John Riselvato over 11 yearsisn't the result of the file extension .html? change your file to .php
-
-
Zach Lysobey over 11 yearsHmmm, honestly I don't know the answers to this stuff. I've never much had to do stuff like this. I guesse more exploration / learning is in order.
-
Brad over 11 years"compile Apache with PHP support"... what?
-
Zach Lysobey over 11 yearsnope, the file on the server is as uploaded. I think maybe inspect element is just doing that
-
kalpaitch over 11 yearsI'd remove the AddHandlers completely, they sound unecessary and probably causing the issue.
-
Zach Lysobey over 11 yearssame with and without... just put that in the question so people saw I was trying things, and didn't suggest something I've already tried
-
Ben Ashton over 11 years@brad Depending on the Linux host... for example: gentoo you will need to add php support when compiling apache. (He did say LAMP didn't he?) :P. Zach: If you want to play around with PHP/Apache on a windows platform, you can download a WAMP version. At this point it seems that your current server configuration has no idea PHP is installed.
-
Zach Lysobey over 11 yearsbeen using XAMPP forever... not new to php, just all these server config issues. OK gotta go for a bit
-
Brad over 11 years@BenAshton, He could be executing PHP via CGI. He hasn't posted his config yet, so it isn't possible to determine.
-
kalpaitch over 11 yearshave you run php -v on the box, as in are you sure you've even got php installed
-
Zagrev over 11 yearsThat's not been my experience. Try 'view page source' too.
-
Zach Lysobey over 11 yearsWell, the code looks "right" in view source. What editor are you using that changes your files after saving?
-
Zagrev over 11 yearsThere are many editors that "help" novice users enter HTML text. These editors automagically "fix" < to <, etc. Just checking, since the result when you inspected the source added comment delimiters to the tag.
-
NULL pointer over 8 yearsI have a file info.php containing <? phpinfo(); ?> and get 'libapache2-mod-php5 is already the newest version' when I try this solution - and the apache server still surrounds my php code with <!-- comments -->
-
James Daily about 7 yearsThank you! I'm using the default built-in PHP webserver, in which php.ini also does not by default permit the short
<?
opening tag (comments it out), and insists upon the full<?php
. -
Zach Lysobey about 5 yearsnaming as .html would certainly make it so that it is not properly understood as php code, but it would not cause the issue of "triggering a file download"
-
Chagai Friedlander over 3 yearsI did not know that this was needed, thanks for clarifying!