PHP/HTML/HTML
Содержание
Display of HTML using PHP code
<source lang="html4strict">
<html> <head> <title>Basic PHP/HTML integration</title> </head> <body> <?
print "PHP
";?> </body> </html>
</source>
Dynamic date insertion
<source lang="html4strict">
<title>PHP | <? print (date("F d, Y")); ?></title>
</source>
Dynamic Generation of <IMG> Tags from an Array
<source lang="html4strict">
<HTML> <HEAD><TITLE>Using the array as a list</TITLE></HEAD> <BODY> <?php
$images = array("image1.jpg", "image2.jpg", "image3.jpg"); foreach($images as $val): ?> <IMG SRC="/images/<?php echo $val; ?>">
<?php endforeach;?> </BODY> </HTML>
</source>
Dynamic HTML tags
<source lang="html4strict">
<html> <head> <title>PHP | <? print (date("F d, Y")); ?></title> </head> <?
$big_font = "h3";
?> <body> <? print "<$big_font>PHP</$big_font>"; ?> </body> </html>
</source>
Dynamic PHP page creation
<source lang="html4strict">
<?
$site_title = "title"; $bg_color = "white"; $user_name = "Name";
?> <html> <head> <title><? print $site_title; ?></title> </head> <body bgcolor="<? print $bg_color; ?>" > <?
print " PHP | ".date("F d, Y")."
Greetings, $user_name!
";
?> </body> </html>
</source>
Example: Creating an XHTML document from PHP
<source lang="html4strict">
<?php if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
header("Content-Type: application/xhtml+xml; charset=utf-8");
} else {
header("Content-Type: text/html; charset=utf-8");
} $doctype = "<?xml version="1.0" encoding="UTF-8"?>"; $head= "<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">"; $head .= " <head>"; $head .= " <title>Document Type Declaration Example</title>"; $head .= " </head>"; $body = " <body>";
$body .= "The content of the page goes here.
";$body .= " </body>"; $footer = "</html>"; echo $doctype; echo $head; echo $body; echo $footer; ?>
</source>
Output Control
<source lang="html4strict">
<?php
print "<html>\n"; print "<body>\n";print "
Welcome, $Name
\n";print "</body>\n"; print "</html>\n"; ?> </source>
Output image tag
<source lang="html4strict">
function html_img($url, $alt = "", $height = 0, $width = 0) {
print "<img src="" . $url . """; if (strlen($alt)) { print " alt="" . $alt . """; } if ($height) { print " height="" . $height . """; } if ($width) { print " width="" . $width . """; } print ">";
}
</source>
Output image tag with global path value
<source lang="html4strict">
function html_img2($file, $alt = "", $height = 0, $width = 0) {
print "<img src="" . $GLOBALS["image_path"] . $file . """; if (strlen($alt)) { print " alt="" . $alt . """; } if ($height) { print " height="" . $height . """; } if ($width) { print " width="" . $width . """; } print ">";
}
</source>
The Header
<source lang="html4strict">
//A sample header file <? $site_name = "PHP"; $site_email = "w@hotmail.ru"; $site_path = "http://localhost/php"; ?> <html> <head> <title> <?=$site_name;?> </title> </head> <body>
PHP |
<? print date ("F d, h:i a"); ?> |
//The Footer
<a href = "mailto:<?=$site_email;?>">contact</a> |
</body> </html> //The Body
<a href = "<?=$site_path;?>/tutorials.php">tutorials</a> |
//Putting It Together: Incorporating the Header, Footer, and Body <? include ("header.tpl"); include ("index_body.tpl"); include ("footer.tpl"); ?>
</source>