What PHP does
Tuesday, 08 December 2009
Article Index
What PHP does
Direct text
Dynamic web pages

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.

Banner

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.

A client side program requires that the browser supports whatever technology you have chosen, Javascript say,  but if you use a server side language like PHP then the browser has to handle nothing but pure HTML.

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:

<?php
echo date('y-m-d');
?>

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:

<?php
echo '<h1> Todays date is: ';
echo date('y-m-d');
echo '</h1>';
?>

If you run this you should see:

 

fig3

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:

<?php
echo '<h1> Todays date is: ';
echo date('y-m-d:h:m:s');
echo '</h1>';
?>

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.

fig4

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.

More PHP

Banner


Developing with PHP and Eclipse (Indigo)

Find out how to make PHP development easy with the latest version of Eclipse, Indigo, and how to set up local and remote debugging.



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 [ ... ]


Other Articles

    <ASIN:1590597311>

    <ASIN:1590595475>

    <ASIN:1593271735>

    <ASIN:1592004946>



    Last Updated ( Monday, 15 February 2010 )
     
     

       
    RSS feed of all content
    I Programmer - full contents
    Copyright © 2014 i-programmer.info. All Rights Reserved.
    Joomla! is Free Software released under the GNU/GPL License.