margarettrias.org

Adventures in PHP

PHP stands for “PHP: hypertext preprocessor.” It is a server-side scripting language used by many web developers to generate code “on-the-fly”. What is PHP used for? This list is by no means exhaustive:

  1. Generating content that is tailored to each user
  2. Eliminating the headache of maintaining static source documents

As an illustration of the second point, let's say you want to create a very large multiplication table. When coding static HTML, you'd have to type out every single table element, and do all the calculations by hand. And any time you decide to change the number of rows and columns, that's five minutes of tedious work!

Here's a multiplication table coded in PHP. Notice how easy it would be to modify the number of rows and columns. Now look at the source code (Right-click > View Source) and see the HTML that was generated.

The code:

echo " <table class=\"mult\"> \n";
for ($i=1; $i<=7; $i++) {
   echo "<tr>";
   for ($j=1; $j<=7; $j++) {
      echo " <td>" . $i * $j . "</td>";
   }
   echo " </tr> \n";
   }
echo "</table> \n";

The result:

1 2 3 4 5 6 7
2 4 6 8 10 12 14
3 6 9 12 15 18 21
4 8 12 16 20 24 28
5 10 15 20 25 30 35
6 12 18 24 30 36 42
7 14 21 28 35 42 49