PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,175 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,582 people online right now. Registration is fast and FREE... Join Now!




PHP to HTML

2 Pages V  1 2 >  

PHP to HTML, Add PHP to a HTML file?

kumaraj

20 Feb, 2009 - 09:56 AM
Post #1

New D.I.C Head
*

Joined: 12 Feb, 2009
Posts: 9

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.

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >  
Reply to this topicStart new topic
Replies(1 - 19)

Christopher Elison

RE: PHP To HTML

20 Feb, 2009 - 10:00 AM
Post #2

D.I.C Head
**

Joined: 29 Dec, 2008
Posts: 226



Thanked: 37 times
My Contributions
A PHP script looks like this:
php

<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 Feb, 2009 - 10:02 AM
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: PHP To HTML

20 Feb, 2009 - 10:24 AM
Post #3

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
php

<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:
CODE
<?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 Feb, 2009 - 10:26 AM
User is online!Profile CardPM
+Quote Post

pr4y

RE: PHP To HTML

20 Feb, 2009 - 11:27 AM
Post #4

Location: 127.0.0.1
Group Icon

Joined: 19 Sep, 2008
Posts: 615



Thanked: 29 times
Dream Kudos: 175
My Contributions
CT, that wasn't the greatest example if you are trying to teach proper PHP to HTML output.

php

<?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 smile.gif
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: PHP To HTML

20 Feb, 2009 - 11:45 AM
Post #5

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
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.gif

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

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

php

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

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

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

?>


User is online!Profile CardPM
+Quote Post

girasquid

RE: PHP To HTML

20 Feb, 2009 - 11:49 AM
Post #6

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 58 times
Dream Kudos: 825
My Contributions
Personally, I wouldn't have PHP be responsible for outputting anything more than is absolutely necccessary:

php

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

User is offlineProfile CardPM
+Quote Post

pr4y

RE: PHP To HTML

20 Feb, 2009 - 11:56 AM
Post #7

Location: 127.0.0.1
Group Icon

Joined: 19 Sep, 2008
Posts: 615



Thanked: 29 times
Dream Kudos: 175
My Contributions
Also, if you need PHP to deliver dynamic content based on the user's actions.. you could do the following:

php

<?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

<?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 Feb, 2009 - 11:58 AM
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: PHP To HTML

20 Feb, 2009 - 11:57 AM
Post #8

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
QUOTE(girasquid @ 20 Feb, 2009 - 02:49 PM) *

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:
CODE
<?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 Feb, 2009 - 11:58 AM
User is online!Profile CardPM
+Quote Post

girasquid

RE: PHP To HTML

22 Feb, 2009 - 01:16 PM
Post #9

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 58 times
Dream Kudos: 825
My Contributions
QUOTE(CTphpnwb @ 20 Feb, 2009 - 11:57 AM) *

But that tends to end up more like this:
CODE
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.
User is offlineProfile CardPM
+Quote Post

no2pencil

RE: PHP To HTML

22 Feb, 2009 - 06:41 PM
Post #10

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,499



Thanked: 303 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
QUOTE(kumaraj @ 20 Feb, 2009 - 11:56 AM) *

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

User is offlineProfile CardPM
+Quote Post

kumaraj

RE: PHP To HTML

23 Feb, 2009 - 05:12 AM
Post #11

New D.I.C Head
*

Joined: 12 Feb, 2009
Posts: 9

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. icon_up.gif
User is offlineProfile CardPM
+Quote Post

girasquid

RE: PHP To HTML

23 Feb, 2009 - 06:02 PM
Post #12

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 58 times
Dream Kudos: 825
My Contributions
You'd save them as .php.
User is offlineProfile CardPM
+Quote Post

mandy011

RE: PHP To HTML

23 Feb, 2009 - 06:24 PM
Post #13

D.I.C Head
**

Joined: 27 Sep, 2008
Posts: 70


My Contributions
why you people fight?? biggrin.gif
User is offlineProfile CardPM
+Quote Post

no2pencil

RE: PHP To HTML

23 Feb, 2009 - 06:25 PM
Post #14

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,499



Thanked: 303 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
uhm....? Where do you see fighting?
User is offlineProfile CardPM
+Quote Post

kumaraj

RE: PHP To HTML

25 Feb, 2009 - 05:53 AM
Post #15

New D.I.C Head
*

Joined: 12 Feb, 2009
Posts: 9

QUOTE(girasquid @ 23 Feb, 2009 - 06:02 PM) *

You'd save them as .php.

Ok. Can IE then open this file if I use Notepad?
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: PHP To HTML

25 Feb, 2009 - 06:40 AM
Post #16

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
Sure, if you want to see the text of the file in IE.

You don't open php files from a browser. You call them through a server, which runs their code and generates html, which it then sends to your browser.

User is online!Profile CardPM
+Quote Post

chris.ballard

RE: PHP To HTML

25 Feb, 2009 - 10:57 AM
Post #17

New D.I.C Head
*

Joined: 25 Feb, 2009
Posts: 14


My Contributions
QUOTE(girasquid @ 23 Feb, 2009 - 06:02 PM) *

You'd save them as .php.


This is only true by default if you are using php4 or newer (which you probably are tongue.gif). You also do not have to name your files .php if you configure your webserver. I have all of mine named .html for ambiguity. If you wish to change your extension and you are using apache you can simply make your AddType (in httpd.conf) look like this:

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


then all you have to do is restart apache and you now have .html registerd as a php extension. Just as a note this does not prevent you from using .html as a strait html file, it just passes the file through the interpreter.
User is offlineProfile CardPM
+Quote Post

KuroTsuto

RE: PHP To HTML

25 Feb, 2009 - 06:03 PM
Post #18

D.I.C Head
Group Icon

Joined: 13 Feb, 2009
Posts: 104



Thanked: 12 times
Dream Kudos: 25
My Contributions
I love how insanely complex this little intro to PHP thread just got tongue.gif. I think I'll ad a lil complexity by restating everything that's been said in my own manner, one which I hope will clear things up a little.

HTML is, in essence, a language that strictly dictates structure and display. It is responsible for how a website is organized, as well as what elements (tables, borders, text, etc.) are what color and essentially where they are. It is then the job of the user's browser (IE, Firefox, etc.) to interpret the HTML and display it correctly.

PHP, on the other hand, is a language that handles logic. While it is true that PHP code is commonly placed in the same document as HTML, when the user requests a file with PHP, the webserver first processes all of the PHP within the file, displaying any additional content that a PHP function tells it to (i.e. the "echo" function essentially passes the content of its argument into the html document*), and then sends the resulting client-side info (i.e. HTML, Javascript, CSS etc.) to the user who originally requested the file. In this sense, while said user did indeed request a page containing PHP code, the page that they actually receive does not contain any PHP code to speak of.

So files with PHP code MUST first pass through a server with PHP installed upon it to function properly, else if you were to open a PHP file on a computer not set up to interpret such code (the vast majority of computers in the world), you would likely see all of the actual code displayed as though it were nothing more than plain text.

To be a little bit more specific about how a server interprets such a file containing both HTML and PHP code, the "<?php" and "?>" tags tell the server where to start and stop processing PHP. Anything inside of those tags will be processed as PHP, and anything outside of them will (in the majority of cases) be left ignored. Hopefully this clears up some of the examples the others have given you.

As for the thing about naming such files with a ".php" extension, I will tell you that by default, most webservers will indeed interpret such a file as one containing PHP by default. This is the safest bet. Or, if you would like to get more advanced and you have access to a webserver or some server install on your own machine, you can, as chris.ballard stated, modify the configuration of the server to use different extensions.

If you're really interested in learning PHP, I will tell you that I first began learning the language from the brief tutorial/overview at the w3schools.com website.

Cheers!
~KuroTsuto
User is offlineProfile CardPM
+Quote Post

pr4y

RE: PHP To HTML

25 Feb, 2009 - 06:09 PM
Post #19

Location: 127.0.0.1
Group Icon

Joined: 19 Sep, 2008
Posts: 615



Thanked: 29 times
Dream Kudos: 175
My Contributions
@ CT - A new PHP developer won't need to worry about the .php4 or .php5 extensions just yet. Although they do add much more to the language that the .php files won't support... I think in this question by the OP, .php is as far as we need to go.



@ The guy who said "What's with all this fighting?" -...... Who was fighting? If you were talking about CT, myself, and a few others discussing the methods in which you can output HTML by using PHP statements... we were doing just that. None of us were arguing, we were just showing the many ways PHP can present information to the end-user.



smile.gif
User is offlineProfile CardPM
+Quote Post

dimjaxor

RE: PHP To HTML

26 Feb, 2009 - 07:07 AM
Post #20

New D.I.C Head
*

Joined: 10 Sep, 2008
Posts: 40


My Contributions
I think all you guys have accomplished is confusing the hell out of him lol.

PHP can be coded many different styles, and it really doesn't matter as long as it works. Sometimes later on someone will find a bug or a security hole (which actually happened to us yesterday), in which you just fix the bug/security hole. In our case we had Javascript doing the validation for a form and didn't even think about doing serverside validation. Until... someone that didn't have javascript enabled on their browser started screwing with the form. Now we make sure we do serverside validation along with our clientside validation. Just another code snippet to add to the collection!

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 07:10PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month