PHP to HTMLAdd PHP to a HTML file?
20 Replies - 2622 Views - Last Post: 26 February 2009 - 08:49 AM
#1
PHP to HTML
Posted 20 February 2009 - 10:56 AM
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.
Replies To: PHP to HTML
#2
Re: PHP to HTML
Posted 20 February 2009 - 11:00 AM
<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
#3
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
#4
Re: PHP to HTML
Posted 20 February 2009 - 12:27 PM
<?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
#5
Re: PHP to HTML
Posted 20 February 2009 - 12:45 PM
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;
?>
#6
Re: PHP to HTML
Posted 20 February 2009 - 12:49 PM
<?php // prints headers, assigns variables, does calculations, etc. ?> <html> <head> <title>title</title> </head> <body> You are visitor number: <?php echo $var ?> </body> </html>
#7
Re: PHP to HTML
Posted 20 February 2009 - 12:56 PM
<?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
#8
Re: PHP to HTML
Posted 20 February 2009 - 12:57 PM
girasquid, on 20 Feb, 2009 - 02:49 PM, said:
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
#9
Re: PHP to HTML
Posted 22 February 2009 - 02:16 PM
CTphpnwb, on 20 Feb, 2009 - 11:57 AM, said:
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.
#10
Re: PHP to HTML
Posted 22 February 2009 - 07:41 PM
kumaraj, on 20 Feb, 2009 - 11:56 AM, said:
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
#11
Re: PHP to HTML
Posted 23 February 2009 - 06:12 AM
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.
#14
Re: PHP to HTML
Posted 23 February 2009 - 07:25 PM
#15
Re: PHP to HTML
Posted 25 February 2009 - 06:53 AM
|
|

New Topic/Question
Reply




MultiQuote





|