PHP/HTML/HTML
Содержание
Display of HTML using PHP code
<html>
<head>
<title>Basic PHP/HTML integration</title>
</head>
<body>
<?
print "<h3>PHP</h3>";
?>
</body>
</html>
Dynamic date insertion
<title>PHP | <? print (date("F d, Y")); ?></title>
Dynamic Generation of <IMG> Tags from an Array
<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>
Dynamic HTML tags
<html>
<head>
<title>PHP | <? print (date("F d, Y")); ?></title>
</head>
<?
$big_font = "h3";
?>
<body>
<? print "<$big_font>PHP</$big_font>"; ?>
</body>
</html>
Dynamic PHP page creation
<?
$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")." <br>
Greetings, $user_name! <br>
";
?>
</body>
</html>
Example: Creating an XHTML document from PHP
<?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 .= " <p>The content of the page goes here.</p>";
$body .= " </body>";
$footer = "</html>";
echo $doctype;
echo $head;
echo $body;
echo $footer;
?>
Output Control
<?php
print "<html>\n";
print "<body>\n";
print "<p>Welcome, $Name</p>\n";
print "</body>\n";
print "</html>\n";
?>
Output image tag
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 ">";
}
Output image tag with global path value
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 ">";
}
The Header
//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>
<table>
<tr><td>PHP</td><td>
<?
print date ("F d, h:i a");
?>
</td>
</tr>
</table>
//The Footer
<table>
<tr><td><a href = "mailto:<?=$site_email;?>">contact</a></td></tr>
</table>
</body>
</html>
//The Body
<table>
<tr>
<td>
<a href = "<?=$site_path;?>/tutorials.php">tutorials</a> <br>
</td>
</tr>
</table>
//Putting It Together: Incorporating the Header, Footer, and Body
<?
include ("header.tpl");
include ("index_body.tpl");
include ("footer.tpl");
?>