Page 1 of 1

php in html?

Posted: Tue May 08, 2007 7:13 am
by dharprog
Hi

I would like to know is there any way to work out the php code snippets in a html pages?

Means i would like to use php code in html pages will it work? if so how? Please tell me how?

THank You.

Posted: Tue May 08, 2007 7:24 am
by CoderGoblin
If your webserver allows you to use PHP then you should be able to use the php code snippets. You may have to change your html file extension from .html to .php but the rest should be able to simpy be dropped in. Recommend you simply try it.

Posted: Tue May 08, 2007 8:23 am
by dharprog
Hi

Thanks for the response.

You mean i have to change the html pages to php pages to work out is it?

What i am asking is without changing the extension of .html to .php pages how can i work out the php code in a html page.

Thank You.

Posted: Tue May 08, 2007 8:29 am
by Grim...
Set your server up to parse .html files with the PHP engine.

http://php.about.com/b/a/257498.htm

Posted: Tue May 08, 2007 8:55 am
by CoderGoblin
To put it very simplistically with an extension of .php (if your webserver recognises it) any php code enclosed in
<?php and ?> tags will be processed. Anything outside of those tags will be simply output as HTML.

Example:

Code: Select all

<?php
  // my "business logic will go here
  $myvar="Well Hello There this is a variable";
?>
<html>
  <head>
  </head>
  <body>
    <p>This HTML will be output</p>
   <?php
       echo "<p>This php echo will also be output</p>";
   ?>
    <p>This HTML will be output</p>
   <?php
       echo "<p>{$myvar}</p>";
   ?>
  </body>
</html>
You may see some examples where only the <? tag is used rather than <?php but this is to be avoided as it relies on certain server settings. Always use <?php.

Normally what you want (ok personal preference) is to try to process as much php as possible at the top of the file and only echo variables within your html. This makes things easier to read. Try to avoid the following:

Code: Select all

<p>This is some starting text</p>
<?php
  if ($somevar == "Eeek") {
?>
     <p>This is shown if variable $somevar is set</p>
<?php
  }
?>
as matching brackets are easily lost.

You may want to start looking at some tutorials on the web to get to grips with this.