Why Are My PHP Tags Converted to HTML Comments

Why are my php tags converted to html comments?

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

PHP gets commented out in HTML

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.

PHP: Code gets turned into HTML !-- Comments? --

That is because it is no longer php.

Change to

<script type="text/javascript" src="base.php"></script>

and have a
<?php header("content-type:text/javascript");
$name = "...";
?>

function showName() {
document.getElementById("name").innerHTML = "<p>Hello <?php echo $name; ?>, How are you?</p>";
}

Or

change to

function showName(name) {
document.getElementById("name").innerHTML = "<p>Hello "+name+", How are you?</p>";
}

and in the php file have

<script>
// using json_encode to make the string safe for script injection.
// Still needs quotes for a single string
showName("<?php echo json_encode($name); ?>");
</script>

HTML auto-converting php tag into comment

<? if ($transaction->payment_method == "boleto") { ?>

Needs to have the correct open tag <?php same with <? } ?> should be <?php } ?>

Why does my PHP appear to be commented out when I view it in the browser? !--?php include(header.php); ?--

when i inspect the element using chrome the php seems to be commented out

The PHP is not passing through a PHP parser before getting to the browser, so the browser is receiving the PHP code instead of the server executing it.

Make sure that:

  • You are loading the page over HTTP (e.g. not just double clicking the file in your file manager)
  • The server you are using supports PHP
  • The server is configured to treat the file as PHP (this is usually done by giving it a .php file extension)

PHP code being commented out

Your PHP contained inside the HTML is using short tags which can be turned off in your php.ini file. Take a look at this directive and make sure it is set to true if you want to use them:

short_open_tag true

http://www.php.net/manual/en/ini.core.php#ini.short-open-tag



Related Topics



Leave a reply



Submit