Read part 1.

When you are putting together a Web site, there are two things you need from
a language:

  • Database access.
  • Web support.

As far as I can tell, PHP’s support for the former is adequate but the Web is
definitely its forte.

I can only talk about PHP’s support for MySQL, but support for other
databases is probably not very different.  As a friend of mine told me not
long ago, "there are not a hundred ways you can retrieve rows from a database".

The pair PHP-MySQL is actually so popular that it’s very likely that if your
ISP supports PHP, they probably installed the MySQL extensions with it, and a
quick way of telling is by invoking phpconfig() and look for "MySQL" in the
result page.

MySQL support is pretty much identical to JDBC:  very low level, you
name columns directly and you reference results by ordinal number.  And
just like JDBC, you need to remember to close the connection when you’re done:

$resultRow = mysql_query($query);
$rowCount = mysql_numrows($resultRow);
for ($i = 0; $i < $rowCount; $i++) {
  $name = mysql_result($result, $i, "name");
  $date = mysql_result($result, $i, "date");

I am sure there are numerous packages built on top of this simple abstraction
but I haven’t done any research yet, and I am purposely trying to keep things
very basic with my code (hence no class or other object-oriented features of PHP
for now, although just using classes would already help separate neatly the
various layers of my application).

The only principle I have found helpful so far is to centralize all the
database-oriented code in one single file, and avoiding to use hardcoded strings
to reference anything in my schemas.  Having said that, I can already
envision some future maintenance nightmare…

Let’s turn to Web support now, which is where PHP really shines.

There are three areas of particular interest to Web developers:

  • Forms
  • Cookies
  • Sessions

And in the three areas, PHP is an example of simplicity.

Consider the following form:

<form action="post.php">
  <input type="text" name="date" />

You collect the value entered in the text field in post.php like this:

$date = $_POST["date"];

Of course, you would use $_GET if that’s the action you are using instead.

Cookies follow a similar pattern:

setcookie("user", "cedric");

// …

if (isset($_COOKIE["user"])) {
  $user = $_COOKIE["user"];

Sessions are stored in an array called, unsurprisingly, $_SESSION.  You
can have one started automatically by PHP or do this explicitly with
.  Of course, the same warnings as in J2EE apply, such
as making sure you keep the number of variables in your session to a minimum
(you can unregister variables with session_unregister()).

If you can put aside the mildly annoying asymmetry in the API (sometimes you
invoke a function, other times it’s a global array), PHP puts a lot of power in
your hands with these simple API’s, and making changes involving an alteration
of a schema and the accompanying change in the business logic and the HTML can
often be made in less than ten minutes.

The next task I’d like to tackle is to research a higher level of abstraction
than what I have been looking at so far, such as template frameworks and
database abstractions.