PHP: A Beginners Guide - Part 2

Wednesday, August 27th, 2008 | PHP

Today I am going to go over how to use PHP within XHTML to generate valid documents.  As I hope everyone knows, an example of an empty Transitional XHTML document is as below.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Basic XHTML</title>
</head>

<body>
</body>
</html>

Let’s take the variables we learned about yesterday and convert the output to valid XHTML:

<?php
$myString = “This is a string”;

$myInteger = 0;
$myDouble = 1.2345;
?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>PHP Part 2: Example 1</title>
</head>

<body>
<p>
<?php
echoYour string is: ” . $myString;
echo<br />“;
echoYour integer is: ” . $myInteger;
echo<br />“;
echoYour double is: ” . $myDouble;
?>
</p>
</body>
</html>

The above output is an example of valid XHTML output.  To see it as a webpage, click here.

There is an echo shortcut in PHP which will make calling variables much easier.  This shortcut is <?= $myVar ?>.  Let’s redo the above example using this shortcut.

<?php
$myString = “This is a string”;

$myInteger = 0;
$myDouble = 1.2345;
?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>PHP Part 2: Example 1</title>
</head>

<body>
<p>Your string is: <?= $myString ?>
<br />
Your integer is: <?= $myInteger ?>
<br />
Your double is: <?= $myDouble ?>

</p>
</body>
</html>

This is all for today.  I know it isn’t much more advanced than yesterday, but I thought I should talk about proper coding.  Tomorrow we will talk about Retrieving data from forms.

Tags: ,

No comments yet.

Leave a comment