Writing PHP Programs
In 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:
- Write and examine a very short PHP program
- Discuss some of the Internet Protocols
- Talk about the role of the PHP engine and the web server
- Define what we mean by interpretation and execution
- Variables - what are they?
- Data Types - we will discuss the different data types that variables can have
- Operations you can perform on variables
- Constants
- Converting variables from one data type to another
- Environment variables
An Example PHP Program
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>
<BODY>
The date at the moment is
<?php echo gmdate("M d Y");
?>
</BODY>
</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: