Can anyone tell me what is the procedure to insert PHP in HTML language?And will file extensiton also change?

Recommended Answers

All 7 Replies

<html>
<body>

<p>
This is a html paragraph
</p>

<?php
echo 'This is some php txt';
?>

</body>
</html>

You will have to name your files with a php extension for the server to recognize them as PHP files and have the PHP parser look for the <?php tags in your file and parse them as php.

Hope this helps.

A bit more on the PHP Extension:

You can also use mod_rewrite in Apache to allow any extension to be parsed as php if you want. (google mod_rewrite) or you could change the mime type associated with an extension on your server (Apache). Apache than assigns a handler for that mime type, and if you associate the extension with the mime type "application/x-httpd-php" it will be parsed as php.

If you are using the include() function in php, it doesn't matter what extension the file is, it will be parsed as php.

I think...

You don't even need the php part in <?php ?>.

Anywhere where you have PHP code instead of HTML, you should use the <? .... ?> to enclose the PHP code.

Also, the file extension should be .php (for example index.php).

You don't even need the php part in <?php ?>.

Anywhere where you have PHP code instead of HTML, you should use the <? .... ?> to enclose the PHP code.

Also, the file extension should be .php (for example index.php).

Just a precaution:

using <? ?> will work for most servers but not all.

Few servers may have the shorthand support turned off. This also stops you from using code like:

<?= ...
<?= '$var ' . ( is_string($var) ? 'IS' : 'IS NOT' ) . ' a string.';

etc.

Another problem is if the server is set up to parse xml files then your <? will be treated as the beginning of <?xml .

So its considered proper form to use <?php ... ?> instead since it should always work on servers with php support.

If you have a script that has alot of shorthand php syntax, and find out it doesnt work on your server, or a server you supplied your php to, then you can use a regex expression to convert your shorthand to the regular syntax.

Example:

$shorthand = '<?php ?> <? echo "blabla"; ?> <?= ?>';
$longphp = preg_replace("/<\?([^p])/i", "<?php$1", $shorthand);
$longphp = preg_replace("/<\?php=/i", "<?php echo ", $longphp);
echo htmlentities($longphp);

This will convert the basic shorthand to regular.
Hope it helps someone. :rolleyes:

If you change the extension from html to php on an existing site, you will be starting all over again with the search engines. That becomes a URL change. If you have an existing site, you probably want to set up the server to run html files through the php interpreter, which is what digital-ether is referring to.

Sorry alpha2006. digital-ether is also correctly stating the best practices method for PHP coding. If transportability is an issue, you should always use php in the opening tag. It avoids a range of problems.

If you are on a Unix or Linux server and can use the .htaccess file, you can set the server to parse html extensions just like php. You just need to add the following to your .htaccess file. Be aware that this may not work if your hosting company does not allow changes to the .htaccess file.

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

If your hosting company allows this change, all files with an html or htm extension will be treated as if they had a php extension.

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

I didn't knew about it

The file extenstion will change in deed.
Open the titile tag..make the changes... close the tag and save the file.
Hope this helps you.
:!:

If you don't want to reconfigure Apache, you can set up 301 redirects from your htm files to .php. Some search engines (I think most of them) will keep your page rank and transfer the records to the new location. Don't use 302 redirects, as that's not the same thing.

301 - Moved Permanently
302 - Moved Temporarily

(This is something that can be done through most webhost's setup.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.