|
|
||
|
| ||
Chapter 2: Beginning PHP 4 December 18, 2000
Writing PHP ProgramsIn the first chapter, we introduced PHP and went through the decidedly non-trivial process of installing it on your web server, and getting PHP itself up and running. What we didn't do in any detail is look at an example of PHP code in action, other than to test that our installation was functioning correctly. The first thing that we will look at in this chapter is how to write a very basic PHP page and get it functioning on our web server, while answering questions about how it works and what the code is doing. Once we've worked through this, we'll examine the role of the web server and find out, in greater detail, how PHP actually works. Once we've established, at a fairly basic level, what goes on under the hood of PHP, we will take a look at some of the principal building blocks of the language, and how we can use PHP to store information in our web pages. Every programming language requires a mechanism for storing information and attaching it to an identifier, which you can then reference for later use. For example, how do you store dates, such as a set of people's birthdays? How can you come back to that list and know which date matches which birthday? This is done in PHP, and in most programming languages, by using the concept of variables. The final part of the chapter will be spent looking at variables and how we can use them to perform mathematical operations, or simple manipulations of text. The itinerary for this chapter is:
This chapter starts with almost the simplest one line example possible - we aim to demonstrate that PHP pages are a mixtures of three things, namely text, HTML code, and PHP script. Pages containing PHP script are different from pages that contain only HTML, and in order to identify them to the PHP engine, they are saved with the .php suffix (or something similar) on the web server and are executed by the PHP engine running on the server. The .php suffix is something that is dependent on your particular configuration of PHP. If you wanted to, when setting up PHP, you could change it to almost anything, even something like .groovysuffix. In this book we will be using the suffix .php in lower case only. The results of this are returned to the browser in HTML. We'll see more on this shortly, but let's get started on an example. Try It Out - First Example Program Open your web page editor of choice and type the following:
<HTML> Save this as example1.php in the document root (on UNIX systems) or root directory (on Windows systems) of your web server. Next, open up your browser of choice, type in the full URL of your web server and web page, mine would be http://chrisu/example1.php. You should see something similar to the following in Navigator 6:
| ||
|
PAGE: 1 I 2 I 3 I 4 I NEXT PAGE |
||


Writing PHP Programs










