PHP to HTML

Add PHP to a HTML file?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 4090 Views - Last Post: 26 February 2009 - 08:49 AM Rate Topic: -----

#1 kumaraj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 80
  • Joined: 12-February 09

PHP to HTML

Posted 20 February 2009 - 10:56 AM

I want to add some PHP to my website. How do I do this?

Do I create a HTML file and add PHP straight to it like this?

<html>
<script language="PHP">
PHP Code
</SCRIPT>

I am not allowed to download any software with the network I am on so I can not download any software!

Please help me.
Is This A Good Question/Topic? 0
  • +

Replies To: PHP to HTML

#2 Christopher Elison  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 230
  • Joined: 29-December 08

Re: PHP to HTML

Posted 20 February 2009 - 11:00 AM

A PHP script looks like this:
<html>
  <body>
    <?php
      echo "Hello world!\n";
    ?>
  </body>
</html>


You'll need to run a PHP script from a web server, and you'll need to make sure your server has PHP installed before you can run PHP scripts, you can't run them from your local file system like you can with HTML/Javascript.

This post has been edited by Christopher Elison: 20 February 2009 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

Re: PHP to HTML

Posted 20 February 2009 - 11:24 AM

<html>
  <body>
    <?php
      echo "Hello world!\n";
    ?>
  </body>
</html>


Yikes! Let's not start'em off with bad form: PHP generates html.

A better way to do it is:
<?php
$myoutput = "<html><body>"; // insert other tags: <head><title> etc.
// code to do something here
// this code will echo $myoutput at some point, followed by
// more html
// end of code to do something
echo "</body></html>";
?>

This post has been edited by CTphpnwb: 20 February 2009 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#4 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: PHP to HTML

Posted 20 February 2009 - 12:27 PM

CT, that wasn't the greatest example if you are trying to teach proper PHP to HTML output.

<?php
$outHeaders = "<html><head><title>Things</title></head><body>";
echo $outHeaders;

$outContent = "<h1>My Page</h1><br/><br/><p>PHP is cool.</p>";
echo $outContent;

$outFooter = "</body></html>";
echo $outFooter;

?>




Fixed organization and consistency in the code :)
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

Re: PHP to HTML

Posted 20 February 2009 - 12:45 PM

Yes, well I was trying to show that php code needs to go ahead of html output, and I guess I didn't do that well either. :blink:

There are loads of examples here where people have had trouble with headers because html preceded php. Here's the most one:
http://www.dreaminco...wtopic87154.htm

I guess I'd add a little to your code:

<?php
$outHeaders = "<html><head><title>Things</title></head><body>";
// code to determine anything
// that must precede html, such as:
// header('Location: [url="http://www.example.com/');"]http://www.example.com/');[/url]
echo $outHeaders;

$outContent = "<h1>My Page</h1><br/><br/><p>PHP is cool.</p>";
echo $outContent;

$outFooter = "</body></html>";
echo $outFooter;

?>


Was This Post Helpful? 0
  • +
  • -

#6 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: PHP to HTML

Posted 20 February 2009 - 12:49 PM

Personally, I wouldn't have PHP be responsible for outputting anything more than is absolutely necccessary:

<?php
// prints headers, assigns variables, does calculations, etc.
?>
<html>
 <head>
  <title>title</title>
 </head>
 <body>
 You are visitor number: <?php echo $var ?>
 </body>
</html>


Was This Post Helpful? 0
  • +
  • -

#7 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: PHP to HTML

Posted 20 February 2009 - 12:56 PM

Also, if you need PHP to deliver dynamic content based on the user's actions.. you could do the following:

<?php
$switch = $_GET['page'];

if (isset($switch)){
    switch ($switch) {
        case 1:
            page1();
            break;
        case 2:
            page2();
            break;
     }
} else {
     defaultPage();
}

function page1(){
     // Deliver page 1 content
}
function page2(){
     // Deliver page 2 content
}
function defaultPage(){
     // Deliver default page content
}



OR

<?php
$switch = $_GET['page'];

if (isset($switch)){
    switch ($switch) {
        case 1:
        ?>
        <!-- HTML Code Here -->
        <?php
            break;
        case 2:
        ?>
        <!-- HTML Code Here -->
        <?php
            break;
     }
} else {
        ?>
        <!-- HTML Code Here -->
        <?php
}
?>



As you can see, with this you could build an entire website off of 1 file. It is not recommended to do this, I just wanted to demonstrate PHP's ability to deliver totally dynamic content to the end user.


I hope we've been helpful!

This post has been edited by pr4y: 20 February 2009 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

Re: PHP to HTML

Posted 20 February 2009 - 12:57 PM

View Postgirasquid, on 20 Feb, 2009 - 02:49 PM, said:

Personally, I wouldn't have PHP be responsible for outputting anything more than is absolutely necccessary:

But that tends to end up more like this:
 <?php
// prints headers, assigns variables, does calculations, etc.
?>
<html>
 <head>
  <title><?php echo $var1 ?></title>
 </head>
 <body>
Some text <?php echo $var2 ?> and some more text <?php echo $var3 ?> and some more....
...
...<?php echo $varN-1 ?>

 You are visitor number: <?php echo $varN ?>
 </body>
</html>


Which is ok I suppose, if you don't mind putting everything into one array for output.

This post has been edited by CTphpnwb: 20 February 2009 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#9 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: PHP to HTML

Posted 22 February 2009 - 02:16 PM

View PostCTphpnwb, on 20 Feb, 2009 - 11:57 AM, said:

But that tends to end up more like this:
 snip


Which is ok I suppose, if you don't mind putting everything into one array for output.


The nice thing about most programming languages is that there's more than one way to do it - typically when I've done this, I've just given the variables meaningful names like $visitors, instead of dumping it all into an array.

Everybody's different, though.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5395
  • View blog
  • Posts: 27,389
  • Joined: 10-May 07

Re: PHP to HTML

Posted 22 February 2009 - 07:41 PM

View Postkumaraj, on 20 Feb, 2009 - 11:56 AM, said:

Do I create a HTML file and add PHP straight to it like this?

Because PHP runs on the server, it cannot simply be included in the html code. The html code is executed by the clients browser, where PHP code does not exist.

[server] -> [internet] -> [ISP] -> [Browser]
PHP Code is executed -> prepares HTML -> Displayed by clients browser
Was This Post Helpful? 0
  • +
  • -

#11 kumaraj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 80
  • Joined: 12-February 09

Re: PHP to HTML

Posted 23 February 2009 - 06:12 AM

Thanks Guys!

kumaraj here again.
If I use any of these codes would I then save these files
as

file.php

OR

file.html

Please help I am completely new to PHP and as I said I am unable to download any software. :^:
Was This Post Helpful? 0
  • +
  • -

#12 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: PHP to HTML

Posted 23 February 2009 - 07:02 PM

You'd save them as .php.
Was This Post Helpful? 0
  • +
  • -

#13 mandy011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 27-September 08

Re: PHP to HTML

Posted 23 February 2009 - 07:24 PM

why you people fight?? :D
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5395
  • View blog
  • Posts: 27,389
  • Joined: 10-May 07

Re: PHP to HTML

Posted 23 February 2009 - 07:25 PM

uhm....? Where do you see fighting?
Was This Post Helpful? 0
  • +
  • -

#15 kumaraj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 80
  • Joined: 12-February 09

Re: PHP to HTML

Posted 25 February 2009 - 06:53 AM

View Postgirasquid, on 23 Feb, 2009 - 06:02 PM, said:

You'd save them as .php.

Ok. Can IE then open this file if I use Notepad?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2