This tutorial assumes:
This tutorial examines a PHP program that implements a form on a website - in this case, a guest book where site visitors can log comments. In addition to providing an overview and working example of PHP, the tutorial introduces Komodo's CGI Debugging functionality. In this tutorial you will:
See Debugging Programs for more information on this Komodo functionality.
On the Start Page under Tutorials and Documentation, click PHP Tutorial, or open the php_tutorial.kpf file from the samples/php_tutorials subdirectory of Komodo's user data directory
The tutorial project will open in the Projects sidebar.
The following components are included in the php_tutorial.kpf project file:
On the Projects sidebar, double-click the file guestbook.php. The file opens in the Editor Pane; a tab at the top of the pane displays the filename.
This section reviews the code in guestbook.php.
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) and that the file guestbook.php is displayed in the Komodo editor.
Komodo Tip: Notice that syntax elements
are displayed in different colors. Adjust the display
options for language elements in the Preferences |
<?php
indicate the start of
the PHP program//
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 30tmp
directory must exist beneath the root
of the drive where the program resides (unless a different
location is specified in the Debugging
Options).
Komodo Tip: On line 23, type
|
class
is a collection of variables and
functionsclass GuestBook
contains the functions
GuestBook
, _getData"
,
outputData
, etcvar
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 25. Notice that the brace changes to a bold red font. The closing brace on line 144 is displayed the same way. In this case, the braces mark the beginning and end of a class. See Editing Files in the Komodo User Guide for more about matching braces. |
$datafile
argument is passed to the
function GuestBook
; multiple arguments are
separated by commas$_SERVER
is a pre-defined PHP variable; it is
passed to the script from the web server$this
; notice that the same syntax is
used to call another functiongb_dat
variable is declared on line
27gb_dat
variable is assigned the value of
$datafile
$this->data
variable is cleared of any
prior value$this->_getData
variable calls the
_getData
function that begins on line 53; when
the _getData
function is complete, processing
returns to line 40
Komodo Tip: On line 38, type
|
REQUEST_METHOD
contained in
$_SERVER
is equal to POST
, processing
passes to the addGuestBookEntry
function on line
120REQUEST_METHOD
is not equal to
POST
, a redirect message is displayed to the
userecho
command generates output\"
are not included inside
the double quotation marks that follow, so that the message
can be displayed as outputPHP_SELF
is the filename
of the current script$_SERVER["PHP_SELF"]
extracts the
PHP_SELF
variable from the
$_SERVER
variableif ($this->data)
statement tests if the
variable $this->data
has a valueoutputData
function and then the outputForm
functiongb_dat
variable into the
$lines
array@
symbol suppresses warnings; in this
case, if the data file is empty, the program generates a
non-fatal errorif ($lines)
statement checks to see if the
$lines
variable has data$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. |
$this->data
variable
are written to the standard output using the echo
statement$_POST
is used to provide
data to the script via HTTP POST!$name
and !$message
, "!"
is a "not" operator; it is true
if either variable is not
true||
symbol is an "or" operator
PHP Pointer: PHP has two "or"
operators: the word "or", and the symbol |
$today
contains the result of the
PHP function date
:date
function returns a stringF
: text monthj
: numeric day within monthy
: four digit yearg
: hour (12 hour format)a
: AM / PM$today
variable and the form datareturn
statement supplies the result (true
or false) of a function or the value of a variable to the
routine from which it was calledfopen
function opens the file stored in
the $this->gb_dat
variablew
switch opens the file if it
existsfopen
will
attempt to create itif !$f
statement checks to see if the
$f
variable contains a valuefwrite
function writes the contents of
$this->data to the file contained in the $f
variablefclose
function closes the file stored in
the $f
variablereturn
statement is tested on
line 112
Komodo Tip: Click on the minus symbol
to the left of line 100. The
entire |
$entry
variable is local to the
addGuestBookEntry
function$entry
contains the contents of the
$data
variable, returned in the
_createEntryHTML
function$entry
are
concatenated with the contents of $this->data
,
and stored in $this->data
$gb
variable creates a new instance of the
GuestBook
class using the file specified in the
$datafile
variableGuestBook
class are
complete, the PHP program is closed using the syntax
?>
This section reviews how to run the guestbook.php program using the Komodo debugger.
In this step you will add breakpoints to the program and debug it. Adding breakpoints lets you run the program in chunks, making it possible to watch variables and view output as it is generated. Before beginning, ensure that line numbering is enabled in Komodo (View|View Line Numbers).
Komodo Tip: Notice that the Debugger Options have been saved from the last time a PHP program was run or debugged. |
Komodo Tip: Debugger commands can be accessed from the Debug menu, by shortcut keys, or from the Debug Toolbar. For a summary of debugger commands, see Debugger Command List. |
GuestBook
class is called from line 148.GuestBook
function.$this
variable on the Locals tab
in the Bottom Pane. Notice that it now has a sub-variable
gb_dat
, which stores the value of the data
file._getData
function.
Continue to select Step In to process each
statement in the function. After line 57 has been processed and
the debugger is stopped at line 58, the $lines
variable can be expanded on the Locals
tab._getData
function. The debugger will proceed to
line 40, which follows the line where _getData
was
called.
Komodo Tip: What do the debugger commands do?
|
addGuestBookEntry
function. On line 121, the
addGuestBookEntry
function calls the
_createEntryHTML
function._createEntryHTML
function, the program assigns
variables to the CGI input data configured in the Debugging
Options._createEntryHTML
function completes, and
processing returns to line 122.addGuestBookEntry
function, until processing moves
to the _writeDataFile
function on line 102.$this->data
variable are written to the
datafile, as displayed in the Watch tab.GuestBook
function.$this->data
variable have been written to the
Bottom Pane.outputForm
function, which writes the HTML
form to the Bottom Pane.ASPN, the ActiveState Programmer Network, provides resources for PHP programmers:
There are many PHP tutorials and beginner PHP sites on the Internet, including: