Changes

Jump to: navigation, search

Creating a Simple PHP Script

74 bytes removed, 17:45, 7 June 2007
no edit summary
In the previous chapter we looked at [[An Overview of PHP|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:
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 than we could never match in this book.
A healthy PHP installation should result in output similar to the following:
== Emdedding PHP into an HTML File ==
As you may have realized, by testing the PHP module on your web server you have already crated crafted your first PHP script. We will no now go on to create another script that is embedded into an HTML page. Open your editor and create the following HTML file:
<pre>
[[Image:Php_simple_example.jpg]]
Of If 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.

Navigation menu