Creating a Simple PHP Script

Revision as of 19:15, 24 May 2007 by Neil (Talk | contribs) (Summary)

Revision as of 19:15, 24 May 2007 by Neil (Talk | contribs) (Summary)

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>';
<?>

Testing the PHP Installation

Before embarking on even the simplest of examples, the first step on the road to learning PHP is to verify that the PHP module is functioning on your web server. To achieve this, we will create a small PHP script and upload it to the web server. To do this start up your favorite editor and enter the following PHP code into it:

<?php
    phpInfo();
?>

This PHP script calls the built-in PHP phpInfo() function, the purpose of which to output information about the PHP pre-processing module integated into your web server.

Save this file as phpInfo.php and upload it to a location on your web server where it will be accessible via a web browser. Once you have done this open a browser and go to the URL for this file. If all is well with your PHP installation you will see several pages of detailed information about your PHP environment covering topics such as how and when the PHP module was built, the version of the module and numerous configuration settings.

If you do not see this information it is possible you do not have the PHP module integrated into your web server. If you use a web hosting company, check with them to see if your particular hosting package includes PHP support (sometimes PHP support is only provided with premium hosting packages so you may need to upgrade). If you run your own web server consult the documentation for your particular type of server (Apache, Microsoft IIS etc) for details on integrating the PHP module. There are vastly superior resources available on the internet to assist in installing PHP that we could never match in this book.


Emdedding PHP into an HTML File

As you may have realized, by testing the PHP module on your web server you have already crated your first PHP script. We will no go on to create another script that is embedded into an HTML page. Open your editor and create the following HTML file:


<html>
<head>
<?php
echo '<title>My First PHP Script</title>';
?>
</head>
<body>
<?php
       echo '<p>This content was generated by PHP</p>';
?>
<p>And this content is static HTML</p>
</body>
</html>

Save this file as example.php and upload it to your web server. When you load this page into your browser you should see something like the following:

Php simple example.jpg

Of you see something like the above in your browser then you have succesfully created and executed your first embedded HTML script.

If you do not see the expected output in your browser window then go back and check your file. Something as simple as a missing character can result in no content being generated by the PHP module. As you will learn with experience, problems are almost always the result of subtle mistake in entering the PHP code.

Embedding HTML into a PHP Script

In the previous example we embedded some PHP scripts into an HTML web page. We can reverse this by putting the HTML tags into the PHP commands. The following example contains a PHP script which is designed to output the HTML necessary to display a simple page. As with the previous examples, create this file, upload it to your web server and load it into a web browser:

<?php
echo "<html>\n";
echo "<head>\n";
echo "<title>My Second PHP Example</title>\n";
echo "</head>\n";
echo "<body>\n";
echo "<p>Some Content</p>\n";
echo "</body>\n";
echo "</html>\n";
?>

When you load this into your browser it will be as if all that was in the file was HTML, and if you use the view page source feature of your browser the HTML is, infact, all you will see. This is because the PHP pre-processor simply created the HTML it was told to create when it executed the script:

<html>
<head>
<title>My Second PHP Example</title>
</head>
<body>
<p>Some Content</p>
</body>
</html>

Summary

In this chapter we have looked at how to test the PHP module is installed into the web server and functioning correctly. We then went on to look at how to add PHP scripts to a web page. In the next chapter we will look at the all important, but all too frequently neglected, topic of adding comments to PHP code.