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
complain function from the authoring module. It's a very simple
function that serves as a good introduction.
You can invoke this function from anywhere in the code. For example,
URL is too long."); takes the global variable
and adds the new complaint to the end, preceded by a line break unless
is currently empty.
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.
If the file exists, the entire text goes into the
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.
|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.|
The value of the
$entry variable prepends onto the main story menu file and
the subject-specific story menu file. So, if the variable
equals "Politics", it prepends onto "Politics.txt".
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
< 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
<i>, and so
eregi_replace function is case-insensitive. You can use
(same name without the "i") for case-sensitive replacement.
The program invokes the following code first when running
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.
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,
complain function (shown earlier) updates the global variable
If everything is okay, the program invokes the
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.