How to insert PHP in HTML language?

Reply

Join Date: Aug 2005
Posts: 15
Reputation: nnhamane is an unknown quantity at this point 
Solved Threads: 0
nnhamane nnhamane is offline Offline
Newbie Poster

How to insert PHP in HTML language?

 
0
  #1
May 19th, 2006
Can anyone tell me what is the procedure to insert PHP in HTML language?And will file extensiton also change?
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to insert PHP in HTML language?

 
0
  #2
May 19th, 2006
  1. <html>
  2. <body>
  3.  
  4. <p>
  5. This is a html paragraph
  6. </p>
  7.  
  8. <?php
  9. echo 'This is some php txt';
  10. ?>
  11.  
  12. </body>
  13. </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...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 116
Reputation: alpha2006 is an unknown quantity at this point 
Solved Threads: 5
alpha2006's Avatar
alpha2006 alpha2006 is offline Offline
Junior Poster

Re: How to insert PHP in HTML language?

 
0
  #3
May 20th, 2006
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).
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to insert PHP in HTML language?

 
1
  #4
May 21st, 2006
Originally Posted by alpha2006
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:

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

This will convert the basic shorthand to regular.
Hope it helps someone. :rolleyes:
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 87
Reputation: TopDogger is an unknown quantity at this point 
Solved Threads: 5
TopDogger's Avatar
TopDogger TopDogger is offline Offline
Junior Poster in Training

Re: How to insert PHP in HTML language?

 
0
  #5
May 21st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 52
Reputation: m-soft is an unknown quantity at this point 
Solved Threads: 0
m-soft m-soft is offline Offline
Junior Poster in Training

Re: How to insert PHP in HTML language?

 
0
  #6
Sep 17th, 2006
Originally Posted by TopDogger View Post
AddType application/x-httpd-php .html .htm
I didn't knew about it
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 10
Reputation: remiremi is an unknown quantity at this point 
Solved Threads: 1
remiremi remiremi is offline Offline
Newbie Poster

Re: How to insert PHP in HTML language?

 
0
  #7
Oct 28th, 2006
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.
:!:
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 494
Reputation: Puckdropper is an unknown quantity at this point 
Solved Threads: 21
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Re: How to insert PHP in HTML language?

 
0
  #8
Oct 28th, 2006
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.)
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC