Basics

A PHP statement is put into a document like this:

<?php echo("This is an echo statement.") ; ?>

A slightly longer example follows:

<HEAD>
<TITLE>PHP Test Page</TITLE>
</HEAD>
<BODY>
<H1>PHP Test Page</H1>
<?php $x=5 ; ?>
<?php echo "<P>x is $x</P>" ; ?>
</BODY>

If you are using Microsoft's FrontPage HTML editor, the above tag format may not work as FrontPage doesn't always deal with short tags properly. But it should work under most other circumstances. With FrontPage, use the following tag format (which is also useful when building longer scripts even if you don't use Frontpage):

<HEAD>
<TITLE>PHP Test Page</TITLE>
</HEAD>
<BODY>
<H1>PHP Test Page</H1>
<SCRIPT LANGUAGE="php">
$x=5;
echo "<P>x is $x</P>";
</SCRIPT>
</BODY>

One of the great things about PHP is that the client never sees your source code - they see only the output, which normally looks like pure HTML. Keep in mind though that if you make an error in your PHP, your methods begin to show:

<HEAD>
<TITLE>PHP Test Page</TITLE>
</HEAD>
<BODY>
<H1>PHP Test Page</H1>
<SCRIPT LANGUAGE="php">
$x=5
echo "<P>x is $x</P>";
</SCRIPT>
</BODY>

Note that I dropped the semicolon after the "$x=5" statement. The result of this error appears as follows (without the fonts displayed in the browser):

PHP Test Page


Parse error: parse error in /home/giles/htdocs/php2.html on line 8

PHP diagnostics like this aren't the most helpful I've ever seen, but they do at least give you a clue as to where the error is. In this case, I'm told "line 8," one line after where the error is - PHP notices the error there because it doesn't find a continuation of the last statement as it expected to (I never told it the last statement ended). Notice that the error names the file where the error occurs, not using the pseudo-file system that HTML uses (this file was called up in the web browser as "http://www.servername.com/~giles/php2.html"), but using the actual Unix file system. This shows the world where your HTML and PHP files are, something that would normally be hidden from view. Any time you code an HTML or PHP page, you should check it for errors - I think this adds a little more incentive.