Analyzing the Program
In this step, you will analyze the PHP program on a line-by-line basis. Ensure that Line Numbers
are enabled in Komodo (View|View Line Numbers). Ensure that the file "guestbook.php"
is displayed in the Komodo Editor.
Lines 1 to 8 - HTML Header
- a standard HTML header is written to the program output
Komodo Tip notice that syntax elements are
displayed in different colors. You can adjust the display options for language elements in the
Preferences dialog box. |
Top
Line 9 - PHP Declaration
- PHP programs are embedded in HTML
- the characters "<?php" indicate the start of the PHP program
Lines 10 to 18 - Comments
- the "//" characters indicate a single-line comment in PHP programs; the
"#" symbol can also be used. Multi-line comments are nested in "/*" and "*/"
characters, as shown on lines 27 to 30
PHP Pointer good PHP code is
liberally annotated with comments. |
Line 19 - Datafile
- the file "guestbook.dat" will be created if it does not exist
- the "temp" directory must exist beneath the root of the drive where the program
resides (unless a different location is specified in the
Debugging
Options)
- PHP statements are terminated with semicolons
Komodo Tip on line 20, type
"$da ". Komodo displays a list of the variables declared above the cursor
position that begin with the letters "da". This is
AutoComplete. |
Top
Lines 22 to 25 - Class Declaration
- a "class" is a collection of variables and functions
- the class "GuestBook" contains the functions "GuestBook", "_getData",
"outputData", etc
- the "var" statement declares variables as "class members", thus making them
portable across functions contained in the class
Komodo Tip click the mouse pointer at the end of line 22. Notice that
the brace changes to a bold red font. The closing brace on line 145 is displayed the same way. In this case,
the braces mark the beginning and end of a class. |
Top
Lines 27 to 34 - Guestbook Function
- a "function" is a discrete block of code
- "$datafile" is an argument passed to the function "GuestBook"; multiple arguments
are separated by commas
- the contents of a function are enclosed in braces
- $HTTP_SERVER_VARS is a pre-defined PHP variable; it is an associative array
passed to the script from the HTTP server
- in PHP, global variables must be declared to be global inside a function if they are
going to be used in that function
- a local variable is defined for the current function by use of the term "$this";
notice that the same syntax is used to call another function
- the gb_dat variable was declared on line 24
- gb_dat is assigned the value of $datafile
- $this-> data is cleared of any prior value
- $this->_getData calls the _getData function that begins on line
51; when the _getData function is complete, processing will return
to line 38
Komodo Tip on line 36, type
"function GuestBook( ". When you type the left parenthesis, Komodo
displays a pop-up hint that describes parameters for the function "GuestBook". This is a
CallTip. |
Lines 38 to 42 - Check for Valid Form Entry
- if the REQUEST_METHOD contained in $HTTP_SERVER_VARS is equal to
"POST", processing passes to the addGuestBookEntry function on line 120
- if the REQUEST_METHOD is not equal to "POST", a re-direct message is displayed
to the user
- the "echo" command generates output
- the characters \" are not included inside the double quotation marks that follow,
so that the message can be displayed as output
- the PHP variable PHP_SELF is the filename of the current script
- $HTTP_SERVER_VARS["PHP_SELF"] extracts the PHP_SELF variable from
the HTTP_SERVER_VARS array
Lines 43 to 44 - Check for Variable Value
- "if ($this->data)" tests if the variable $this->data has a value
- the program will execute the outputData function, then the outputForm function
Top
Lines 51 to 56 - _getData Function
- the "file" statement parses the contents of the file stored in the gb_dat variable
into the $lines array
- the "@" symbol suppresses warnings; in this case, if the data file
is empty the program generates a non-fatal error
- "if ($lines)" checks to see if the $lines variable has data
- the "join" statement converts the $lines array to a string and places it in the
variable $this->data
PHP Pointer use the @ operator with
care; you could disable error messages for critical errors that terminate the execution
of the script. |
Top
Lines 62 to 64 - outputData Function
- the contents of the $this->data variable are written to the standard output
using the "echo" statement
Top
Lines 70 to 77 - Retrieve Form Data
- the PHP variable array HTTP_POST_VARS is declared to be global within
the current function
- lines 74 to 77 extract the form data and place the items in variables
Lines 80 to 83 - Validate Form Data
- on line 80, the validity of the name and message variables is tested
- in "!$name" and "!$message", "!" is a "not" operator; it is true
if either variable is not true
- "||" is an "or" operator
PHP Pointer PHP has two "or" operators:
the word "or", and the symbol "||". The two operators have different levels of precedence,
thus providing flexibility in logic tests. |
Line 86 - Current Date and Time
- the variable "$today" contains the result of the PHP function "date"
- the "date" function returns a string
- the "switches" are interpreted as follows:
- F: text month
- j: numeric day within month
- y: four digit year
- g: hour (12 hour format)
- a: AM / PM
Lines 89 to 94 - Interpolate Form Data with HTML
- text and HTML tags are parsed with the $today variable and the
form data
- the "return" statement supplies the result (true or false) of a function or the value
of a variable to the routine from which it was called
Top
Lines 100 to 106 - Open the Data File
- fopen opens the file stored in the $this->gb_dat variable
- the "w" switch opens the file if it exists
- if the file does not exist, fopen will attempt to create it
- the file is opened for writing only, and the file pointer is positioned
at the top of the file
- !$f checks to see if the $f variable contains a value
Lines 108 to 110 - Write to the Data Files
- fwrite writes the contents of $this->data to the file contained in the $f variable
Lines 111 to 113 - Close the Data File
- fclose closes the file stored in the $f variable
- the value of the "return" statement is tested on line 112
Komodo Tip click on the
minus symbol to the left of line 100. The entire _writeDataFile function
will be collapsed. This is
Code
Folding. |
Top
Lines 120 to 125 - Call Functions for Writing Data
- the $entry variable is local to the addGuestBookEntry function
- $entry contains the contents of the $data variable, returned in the
_createEntryHTML function
- on line 123, the contents of $entry are concatenated with the contents
of $this->data, and stored in $this->data
Top
Lines 127 to 132 - Function for HTML Form
- the PHP variable HTTP_SERVER_VARS is declared to be global within this
function
Lines 133 to 142 - the HTML Form
- these lines generate a standard HTML form
- notice the PHP snippet on line 134 that provides the program name
to the HTML output
Top
Lines 149 to 152 - Closing Tags
- the $gb variable creates a new instance of the GuestBook class using the file
specified in the $datafile variable
- when the functions in the GuestBook class are complete, the PHP program is closed
using the syntax "?>"
- closing HTML tags are written as output
|