Page 3 of 3
Today's web page
At this stage of describing PHP it is always difficult to find a convincing example that doesn't use facilities that are much more advanced.
The whole point of PHP is to generate dynamic web pages and in most cases this involves the use of a database or something to retrieve data that can then be rendered as a suitable web page.
For an example of something that you can do using just HTML consider creating a page that shows the user the current date and time. This is something that cannot be done using nothing but pure HTML - you need either a client side or a server side program to generate the text represenation of the current date and time.
The key to creating a web page that displays the current date is to use the PHP command date. This is, strictly speaking, known as a function and later you will discover how to extend PHP by adding your own custom commands or functions. For now all you need to know is that the date function returns the current date formatted as you specify.
For example, date('y-m-d') gives the data formatted with a two-digit year, month and day value separated by dashes e.g. 09-02-01 which is the 1st February 2009.
The simplest program that will display today date is:
but this doesn't look very impressive. If you run it you should see the date in a default font. A slightly more complicated program might include some text to announce the date and could enclose everything in <h1> tags to make it use a headline style:
echo '<h1> Todays date is: ';
If you run this you should see:
What is important about this example is that you can see that the program generates the following HTML:
<h1> Todays date is: 09-12-07</h1>
and you should be able to see where each element of the web page comes from.
Also notice that once generated the HTML doesn't change - this is not a "live" display of the date in the browser. You can see this more clearly is you also include a specification to display the time:
echo '<h1> Todays date is: ';
This now displays the date with the hours, minutes and seconds. Notice that the seconds do not update as you look at the web page only when then web page is refreshed and the PHP program is run again is the date and time up-dated.
In a real example we would need a lot more PHP to generate a lot more HTML - the HTML in this example isn't well formed as it doesn't have a header or a body or quite a few other things that every well formed web page should have. The point is that it is a simple example stripped down to just what is necessary to be an example.
What have we learned.
- A PHP program is run when a client asks for it to be displayed as if it was a web page.
- Any output that the PHP program generates is sent to the web browser and hence treated as a web page.
- Anything between </php ?> is treated as a list of PHP instructions which are obeyed one after another.
- Each PHP instruction ends with a semicolon
- Anything not between </php ?> is simply sent to the client browser as output of the program.
- The echo instruction sends what ever follows it (in quotes) to the output.
- The date function can be used to generate the date and time when the PHP program is running.
PHP Inner Functions And Closure
PHP inner functions and anonymous functions are a little strange to say the least. However, just because something is strange doesn't mean that it isn't useful. We take a close look at the way PHP fun [ ... ]
Object-oriented HTML Generation In PHP
PHP is a programming language specifically designed to generate HTML pages but one of the topics that is rarely discussed is exactly how to do this. In this article the idea that HTML generation shoul [ ... ]