Difference between revisions of "Creating a Simple PHP Script"

From Techotopia
Jump to: navigation, search
(Testing the PHP Installation)
Line 17: Line 17:
 
== Testing the PHP Installation ==
 
== Testing the PHP Installation ==
  
Before emarking on even the simplest of examples the first step on the road to learnign 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 to the web server. To do this start up your favorite editor and enter the following PHP code into it:
+
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:
  
 
<pre>
 
<pre>
Line 27: Line 27:
 
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.
 
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 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 UTL 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.
+
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.
 
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 ==
 
== Emdedding PHP into an HTML File ==

Revision as of 13:50, 24 May 2007

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