As a language for building dynamic Web pages, PHP provides a simplified way to construct complex and powerful web-related programs. Erik uses a primitive, real-world site example to step through the fundamentals of PHP. This series of articles is divided into two parts, the 1th part introduces the basic concept of PHP, and analyzes an application example, webzine. webzine includes an edit page for the content provider to enter the text of the article, as well as a front end for presenting the content to the world.
If you are in contact with PHP for the first time, you may be pleasantly surprised to find out how easy it is to use in practice. This article is intended to give you a good impression of how PHP works, and then you can determine if it is right for you.
1. Experience with PHP
This series of articles is divided into two parts, and part 1th describes how the application works (not including PHP installed on your system). You'll have the opportunity to try out the main index page and see how some of the components work behind the scenes. If this is your only motivation, be sure to check out the next article, which will delve deeper into the rest of the main index page. (Part 2nd also discusses the editing component with several program examples.) You can download the source code and put your own ideas into practice. )
If you understand HTML and are familiar with any C-like language (especially Perl), you understand that the following example should not be a problem. Even if you don't have too many languages like C, you can still understand these examples. But you do need to understand the basics of HTML.
This PHP application sample is a webzine that generates a reader with a simple send module (code not exceeding 3K). The module displays a list of topics.
Under each topic title is a series of article summaries in reverse chronological order. The user can see the entire article by clicking on the title. There is also a slightly more complicated editing module that allows any reader to be the author and submit his/her own content. The author must select a category and enter the title of the article, a simple summary, and the full text of the story. If you prefer, you can enter the URL of an image file and click the Preview button to verify that everything is ready. The author's input is validated-even through security checks-that will convert all tags except a few security tokens into an inactive format, preventing dangerous or malicious HTML coding from being executed. For example, the string will become. This conversion actually invalidates the token.
2. A dedicated PHP technology
Before studying the actual code for webzine, let's consider a simple example that illustrates the characteristics of PHP very well. PHP syntax allows you to arbitrarily mix HTML statements with PHP statements. This means that HTML statements can appear in the context of loops, if/else statements, functions, and so on. I took advantage of this feature in the webzine program, but the following code sample makes it more concise in a simpler way.
Suppose we have two arrays, $names and $days, which contain information for each month of the year, so $days[0]= 31 and $names[0]= "January", $days [1] equals 28 and $names[1] equals "February", and so on. The following technique is used to create a table with a number of days and month names:
Listing 1: Creating a table with days and months names
Name |
| Days
for ($i =0; $i <12; $i + +) {//cycle start.?>
|
|
}//Loop end.?>
For clarity, the above PHP statement is shown in red, and the HTML statement is displayed in black. Note that the private tag switches from HTML to PHP, while?> switches back to HTML.
One thing to note is that you can switch to PHP, start a For loop (or if/else, or switch, or other statement), and then go back to HTML, and now the HTML commands you are entering will become part of the looping structure, so they repeat with the loop. You can enter or exit PHP mode as needed (for example, by emitting an echo statement to output a variable to a Web page, as I did above). Then, when you return to PHP and enter a closing curly brace, as you expected, the loop ends.
The actual table looks as follows:
Name days
January 31
February 28
March 31
April 30
May 31
June 30
July 31
August 31
September 30
October 31
November 30
December 31
If this behavior is confusing, consider it this way: the PHP interpreter replaces each line in the HTML schema with the Echo statement to feed the row into the output stream. If the Echo statement appears in the IF/ELSE structure, it will be conditionally executed. If it appears in a looping structure (as shown above), it will be executed repeatedly.
3. Application Overview
The webzine driver, INDEX.PHP3, has three main components: a Theme menu, a story list, and a complete story presentation. If the reader chooses a theme that does not have any stories, some default text is displayed. Editing pages is more complex. It contains a form, a feedback message, and a confirmation message that the form is used to accept the user's input, a feedback message to inform the author of the problem to be corrected, and a confirmation message to show the author what they have submitted. It also knows how to validate the submitted story, make the necessary changes to ensure that the content does not contain any unapproved HTML, save the story in a story file, and update the corresponding menu file for the story.
The application has three data files: The Category.txt contains a simple list of topics that the story is organized here. Each topic is associated with a theme menu file. The first theme must be "Main" and be associated with the theme menu file Main.txt. If the second topic is "Thearts", it is associated with a theme menu file named TheArts.txt. In a menu file, each story has a single line of information: A story number, a title, a category, a simple summary, and an optional image URL. Finally, the story file contains the actual body of a story. The file S1.txt will contain the first submitted story, S2.txt contains the second submitted story, and so on. If you know the number of a story (for example, number 26th), its filename is easy to determine (S26.txt).
4. Trial webzine!
Before you study this application carefully, try it out first. Try the webzine driver. Its left side is a list of topics, and the right side is a list of stories. You can select a theme, or select "Main" to see all the stories. The first thing that comes up is the list of recent stories, and the first set of stories, if they have images, they are also displayed. When you click the title of a story, a page appears that contains the full body of the story.
Try the edit page (you can also try it from webzine). It provides a form that allows you to submit a story. When submitting content to webzine, please pay attention to the concept of the public and good taste. If you enter content that the program does not like, an error message is displayed. Once the story is accepted, you can return to webzine to see what it looks like in front of the reader.
Now that you have tried the application, continue reading the sections that follow to learn about its creation.
4.Webzine drivers
Passing parameters
The INDEX.PHP3 page allows parameters to be passed as follows: index.php3?topic=tradeshow&story=33
For the above call, the following variable assignment will be performed automatically before calling Index.php3:
$topic = "tradeshow";
$story = 33;
If you omit these parameters, the $topic and $story variables will not exist. You can explicitly detect them, or let PHP return the default null value when you reference them.
Note: If this feature does not work on your system, please check the php.ini file to make sure register_globals = on.
Page title
Let's first look at a common technique in many PHP applications, and put some kind of information in variable assignment statements at the beginning of the program. This is easy to maintain and update later.
Listing 2: Assigning Values to variables
Again, notice the boundary tag for PHP: You get into PHP mode from HTML mode, and?> switches back to HTML mode. You can switch back and forth any time. Some operations are easier to implement in HTML mode, while others are easier to implement in PHP mode. All you do is define two variables at the beginning of the program and then go into HTML mode. When a variable needs to be used, the PHP mode is returned and an echo statement is issued to write the value of the variable directly into the body of the page.
$title = "PHP Demo webzine";
$slogan = "Illustrating the coolness of PHP since September 2000";
?>
<title><?php Echo ($title)?></title>
Category Menu
The Category.txt file is just a list of subject names, and each topic list is exclusively one line. For example, if the file contains only three items:
Main
Politics
Technology
You will get three themed menu files: Main.txt, Politics.txt, and Technology.txt. Once the "Main" theme is selected, the driver appears as follows:
Main
Politics
Technology
The HTML source code looks like this:
Here is the PHP from which the Category.txt is compiled into the HTML snippet shown above
Snippets of code that are extracted from the source code. First, Category.txt the file
Read into an array $cats:
$cats = File ("Category.txt");
$elems = count ($cats);
?>
The file function simply copies the files into an array. So $cats[0] equals "Main", $cats [1] equals "politics", $cats [2] equals "technology". The file function makes it easy to import a small ASCII text file, but do not use it for very large files. The Count function counts the number of elements in the array, so in this case $elems should be equal to 3. Here's how to use that array to create the above HTML table.