1. Introduction
Chapter Aims
After reading this chapter you should be able to:
understand the value of using the PHP language to enable non-static information to be included on webpages, especially information retrieved from a relational database using the popular database query language MySQL
understand how a web browser processes an HTML file and how PHP files are processed
check that your web server is able to run PHP scripts
understand how a valuable improvement to a webpage can sometimes be made using only a single line of PHP.
PHP is a programming language designed for the age of the World Wide Web. Originally all web pages were written using HTML (HyperText Markup Language). HTML was (and is) a language that enables information to be displayed over the Internet by a standard piece of software, known as a web browser, in a very flexible way. It was developed by the British scientist Tim Berners-Lee in the early 1990s and the rest is very well-known history.
Freely available web browsers and web page authoring tools combined with broadband telephone connections in most offices and many homes, the availability of WiFi wireless internet connections, high-powered search engines, etc. has made the WWW one of the most influential (although almost entirely unpredicted) developments of the late twentieth and early twenty-first centuries, providing information at zero or minimal cost worldwide to businesses, individuals in the home and travellers using mobile devices. Increasingly the web is used as the medium of choice for buying (or often just downloading free of charge) music, books and films as well as for booking holidays and buying goods.
The range of applications is too wide and too well-known to need labouring here and will in any case expand yet further as time goes by. However for many applications basic HTML is not enough.
Figure
Fig. 1.1
A simple webpage
The problem with such a webpage is that it is the same every time it is seen and for all possible users. The millionth time it is viewed it will look just the same as the first time (we will ignore possible slight incompatibilities between different web browsers). Every detail of its content and layout is as specified by the writer of the HTML file. We will call this a static webpage .
We have to admit that for a very large number of webpages none of this is a problem at all. However, there are at least three reasons why for some purposes it may not be satisfactory.
(1)
We may wish to include information that varies from one viewing of a web page to another, e.g. the current date and time.
(2)
The user may wish to provide information for a web page to use, e.g. he or she may wish to specify a location and a range of prices for which a list of holidays should be displayed or the name of an author for which a list of available books should be displayed.
(3)
We may wish to include information that varies from one user to another, e.g. details of holidays or aeroplane flights booked or a household account with an energy provider, or that changes frequently, such as the prices of shares owned by an investor in a stock market.
Requirements (1) and (2) cannot be met by HTML alone. Requirement (3) could be met provided the writer of the HTML file could be persuaded to update it with new share prices, perhaps several hundred or even several thousand times a day. It would obviously be far more convenient if the file could be left unaltered but information such as current prices could somehow be extracted automatically from a database and inserted in the right place when the corresponding webpage was displayed.
This can certainly be done and is done every day by the well-known e-commerce websites and many others, but it cannot be done using HTML alone. It needs augmenting with facilities to create, maintain and search databases and to customise the pages displayed to the needs of individual users. PHP is one of a number of programming languages that have been developed to work with HTML to give this considerably enhanced functionality. Compared with many other computer programming languages it is easy to use and makes building even quite elaborate applications straightforward to do.
Using PHP extends the facilities available in HTML considerably, especially when used in conjunction with a database query language such as MySQL. Web pages are still written in HTML but parts of the HTML are created automatically from PHP 'insertions' in the HTML code by a special program known as the PHP 'interpreter'. This is located on the web server, which is why PHP is called a server-side programming language. The PHP programmer needs no special software on his or her PC. The user of a page written with PHP needs just a standard web browser and will generally be entirely unaware that a page was not originally written in HTML except that it will often be possible to do more with it.
As an example, a travel company may wish to advertise 500 different holiday locations. It could do this by writing 500 different web pages, one per location, but this would be very tedious to do and the pages would inevitably all have common features, such as the name of the company, a hyperlink to a booking form, etc. Alternatively the company could write one 'generic' web page, giving its name, address, etc., which displays the information about one of 500 locations taken from a database depending on options selected by the user. This latter option is clearly far more attractive to the company and storing the information in a database will probably make it far easier to provide the user with good search facilities.
This book is about using PHP to enhance the functionality of webpages, especially but not exclusively by providing facilities to create, maintain and interrogate databases. PHP is not the only programming language that can be used with HTML and MySQL is not the only database query language, but this combination is one of the most popular and widely available. PHP can also be used very effectively without MySQL to give a similar effect to having a database available using merely a plain text file on the server as will be illustrated in Chap.. PHP is easy to use and has many powerful features. The language was invented by Rasmus Lerdorf in 1994 as an aid to maintaining his personal webpage. It has since expanded into a very powerful general-purpose programming language. The name PHP originally stood for Personal Home Page, but we are now told that PHP stands for 'PHP: Hypertext Preprocessor'.
A note on terminology: Programs written in PHP or similar languages are generally called scripts rather than programs and the languages are generally called scripting languages rather than programming languages . Those familiar with other languages will soon realise that PHP is not just a programming language in the standard usage of the term but a very well-designed and powerful one, which has several unusual features. In this book we will use the terms 'program' and 'script' interchangeably. For the benefit of those readers who know the difference, PHP is an interpreted language not a compiled one.
This book is about PHP and its use with the MySQL gives basic information about the most important HTML tags.