Creating a Simple PHP Script

From Techotopia
Revision as of 13:37, 24 May 2007 by Neil (Talk | contribs) (The PHP Code Delimiters)

Jump to: navigation, search

In the previous chapter we looked at how PHP works. No technology book would be complete without including the obligatory simple example, and PHP Essentials is no exception to this rule.

In this chapter we will look at constructing the most basic of PHP examples, and in so doing we will take two approaches to creating PHP powered web content. Firstly we will look at embedding PHP into an HTML page. Secondly, we will look at a reverse example whereby we embed the HTML into the PHP. Both are perfectly valid approaches to using PHP.

The PHP Code Delimiters

The first thing to understand is the need to use PHP code delimiters to mark the areas of PHP code within the web page. By default, the opening delimiter is <?php and the closing delimiter is ?> (later in this chapter we will look at changing these delimiters). You can insert as many insert as many or as few blocks of PHP into a web page as you need as long as each block is marked by the opening and closing delimiters.

A sample PHP script block would, therefore, appear in an HTML file as follows:

<?php
echo '<p>This is a PHP script</p>';
<?>

Emdedding PHP into an HTML File