PHP Examples

On a server that supports PHP, it is not possible to simply save the PHP files from your browser. Instead, you can download all examples on this page in a single tarball.

Example 1: PHP info

The first example is trivial. It prints some information about PHP and the web server.

Example 2: Thumbnail images

This example creates an index of JPEG pictures by scanning the directory and creating a page of small pictures.

We start by scanning the directory and create a list of all files in the First stage.

The next step is selecting all JPEG pictures. We use a regular expression match to test if the filename ends in ".jpg".

Now we have all JPEG files, we create fixed-size images. One HTML <IMG> tag for each filename. The Third step shows this.

To show the full picture by clicking the thumbnail, the fourth step creates a link for each image.

Example 3: A title index

This example creates a directory index from the titles in the HTML files in the directory. It is like an automatically generated index, except the filenames are replaced by the content of the <title> tags.

The example is presented in 5 phases, each phase adding a bit more functionality. We start by scanning the directory and create a list of all files in phase 1

Since we don't want all files, just the HTML pages, we check if the filename ends in ".html". As shown in phase 2, a regular expression match is used on the filename.

Now that we have selected the HTML files from the directory, we need to find the title tags. In phase 3, we open each HTML file and read the whole file line by line.

Now we have found the <title> tag in the HTML file, we can read the contents of the title. In phase 4, we store the content of the title until the </title> is found.

Finally, in phase 5, we create a link for each title.

More examples

date function demo

A php created image

A web-based database example