Developer Forums | About Us | Site Map


Useful Lists

Web Host
site hosted by netplex

Online Manuals

Script Web databases quickly with PHP
By Craig Knudsen - 1999-09-01 Page:  1 2 3 4 5 6 7 8

Simple Scripting

Once you install PHP you can try scripting a simple database to get a feel for how it works.

All the programming examples in this article work with both PHP 3 and the beta version of PHP 4.

Standard PHP scripts begin with <?php. And they end with ?&gt;. All PHP variables start with $ just like in Perl.

Also as in Perl, there's no need to declare variables before using them. You embed all the PHP code within the HTML script, so you can use your favorite text editor to edit PHP files. There's even optional support for Microsoft's FrontPage.

You can switch between HTML and PHP scripting as much as you like within a file. For example, note the PHP mixed with HTML in the code in Listing 1, which shows how to create conditional HTML. The ereg function in line 3 of the listing does regular-expression matching on the browser identifier ($HTTP_USER_AGENT) looking to find "MSIE."

Note that the syntax of the if/else statement is identical to C's syntax. For more information on PHP functions used in the example listings, check the online PHP Manual (see Resources).

Listing 1. PHP embedded within HTML generates different Web content for users with Microsoft Internet Explorer

Listing 1
 1   <HTML>
 2   <?php
 3     if ( eregi ( "MSIE", $HTTP_USER_AGENT ) ) {
 4   ?>
 5     You are using <B>Microsoft Internet Explorer</B>!
 6   <?php
 7   } else {
 8   ?>
 9     You are not using Microsoft Internet Explorer.
10   <?php
11   }
12   ?>

13   </HTML>

Switching between HTML and PHP is very handy, but it is sometimes difficult to read. You could rewrite the script in Listing 1 more legibly by using the print function, as shown in Listing 2.

Listing 2. This script performs the same function as Listing 1, but it's easier to read

Listing 2
 1   <HTML>
 2   <?php
 3     if ( eregi ( "MSIE", $HTTP_USER_AGENT ) )
 4       print ( "You are using <B>Microsoft Internet Explorer</B>!\n" );
 5     else
 6       print ( "You are not using Microsoft Internet Explorer.\n" );
 7   ?>

 8   </HTML>

View Script Web databases quickly with PHP Discussion

Page:  1 2 3 4 5 6 7 8 Next Page: Setting Up The Database

First published by IBM developerWorks

Copyright 2004-2019 All rights reserved.
Article copyright and all rights retained by the author.