Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > PHP BY EXAMPLE: PART 2


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

PHP by example: Part 2
By Erik Zoltan - 2004-06-29 Page:  1 2 3

Authoring page "author.php3"

I won't go into nearly the same level of detail about the authoring page because it is longer and I've already discussed many of the underlying concepts.

As a simple illustration of what a PHP function looks like, look at the complain function from the authoring module. It's a very simple function that serves as a good introduction.

// Informs the user of an input problem.
   function complain($problem) {
      global $status;
      if ($status != "") $status = $status . "<br>\n";
      $status = $status . $problem;
   }

You can invoke this function from anywhere in the code. For example, complain("The URL is too long."); takes the global variable $status and adds the new complaint to the end, preceded by a line break unless $status is currently empty.

Prepend

The prepend function is one of the most important in the authoring module. It adds a new entry to the first line in a file. Here, I use it to add the author's new submission to the beginning of the topic menu file.

// Add a new line to the start of an existing file.
   function prepend($file,$string) {
      // TO DO - use database to make this algorithm more scaleable.
      //         Current limit is 100 stories.
      if (file_exists($file)) {
         $filetext = file($file);
         $lines = count($filetext);
      } else {
         $lines = 0;
      }
      if ($lines > 100) $lines = 100;
      $handle = fopen($file,"w");
      fputs ($handle, "$string\n");
      for ($i=0; $i<$lines; $i++)
         fputs ($handle, $filetext[$i]);
      fclose ($handle);
   }

If the file exists, the entire text goes into the $filetext array. The lines are then counted. If the file doesn't exist, it's treated as having zero lines. One hundred lines is the limit of the length (actually 101 because of the first line added before counting starts). The file opens, the program writes the current string first, and then writes the remaining (up to 100) lines to the file. Anything after those additional 100 lines just drops off the end.

This function relies on several important file-handling functions that are built into PHP. The following table describes what each function does.

FunctionDescription
file_exists(string)Uses the string as a filename, and returns true if the file currently exists.
file(string)Using the string as a filename, returns the text of the file in an array where each line is a separate array element. Note that the line termination character(s) will be at the end of each line in the array.
count(array)Returns the number of elements in the array.
fopen(string, mode)The string is a filename, and the mode is a string such as "w" to write or "r" to read. The file will be opened, and a handle will be returned.
fputs(handle, string)Writes a string to the specified output file. You must specify a file handle, you can't specify a filename in string form.
fclose(handle)Closes the file, terminating output.

Save

      $entry = "$storynum^$storytitle^$storyimg^$synopsis";
$storynum$storytitle$storyimg$synopsis
      prepend("Main.txt",$entry);
      $topicfile = "$subject.txt";
      prepend($topicfile,$entry);

The value of the $entry variable prepends onto the main story menu file and the subject-specific story menu file. So, if the variable $subject equals "Politics", it prepends onto "Politics.txt".

      // Selective Security!  Convert most HTML tags to text.
      $storytext = eregi_replace("<", "&lt;", $storytext);
      $storytext = eregi_replace("&lt;b>", "<b>", $storytext);
      $storytext = eregi_replace("&lt;i>", "<i>", $storytext);

There are a lot of search-and-replace statements that apply to the story text; you'll see just a few of them above. Replacing the < symbol with the HTML symbol &lt; will prevent the author from entering any risky HTML tags into the document. However, since we want to give the author some formatting capabilities, we then replace &lt;b> with <b>, &lt;i> with <i>, and so on. The eregi_replace function is case-insensitive. You can use ereg_replace (same name without the "i") for case-sensitive replacement.

Initializations, setup

The program invokes the following code first when running author.php3:

if ($submit == "Submit") {
      $status = "";
      if (!isset($storyimg)) $storyimg = "$subject.gif";
      validate("Subject",$subject,1);
      validate("Story Title",$storytitle,10);
      validate("Synopsis",$synopsis,50);
      validate("Story Text",$storytext,300);
      if ($status == "") {
         $status = "complete";
         save();
      }
   }

If the user clicks the submit button to invoke the form (as opposed to the "Preview Image" button which is also a submit button), the input is validated. The validate function checks each field to make sure it is not null, and that it is not too short. (A maximum-length feature would be a good enhancement to add here.) If there's a problem, the complain function (shown earlier) updates the global variable $status. If everything is okay, the program invokes the save function to save the author's input.

Why you should use PHP

This article is intended to give you a feel for what it's like to program in PHP, especially since many developers still haven't fully committed to a Web scripting language. I've only discussed the features of PHP necessary to create this specific application, so these aren't just theoretical features that you're unlikely to use in practice.

PHP is an attractive choice for Web application development for many reasons. Here are a few that come to mind based on this example:

  • The integration of PHP and HTML together into a single source document makes it easy to add PHP to existing Web pages. It also means you can prototype an idea in HTML and add PHP to it later.
  • PHP programs are easy to understand because their structure inevitably mirrors the structure of the Web pages they produce. For the same reason, it's also pretty easy to design in PHP.
  • As you can see from this application, the file-handling and string-handling functions were designed with ease-of-use in mind. This is apparently a general design philosophy behind PHP -- it doesn't conform to someone's theoretical model of a programming language; it's easy to use in practice.
  • PHP is very approachable. It's easy to learn, and easy to get started writing great Web apps. You don't face a big learning curve. Once you know the basics, all you need is a manual and you're ready to hit the ground running.
  • PHP is also great with databases, but that's another article.

I hope you're excited about using PHP. Feel free to use and customize the source code contained herein.



View PHP by example: Part 2 Discussion

Page:  1 2 3 Next Page: Resources

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.