PHP Tutorial Komodo IDE only


Overview

Before You Start

This tutorial assumes:

PHP Tutorial Scenario

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:

  1. Open the PHP Tutorial Project and associated files.
  2. Analyze guestbook.php, the PHP program included in the PHP Tutorial Project.
  3. Run the program and generate HTML output by running the program.
  4. Debug the program using the Komodo debugger.

See Debugging Programs for more information on this Komodo functionality.

Opening the Tutorial Project

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.

Overview of the Tutorial Files

The following components are included in the php_tutorial.kpf project file:

Open the PHP Tutorial 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.

Analyzing the PHP Tutorial File

This section reviews the code in guestbook.php.

Analyzing guestbook.php

Introduction

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.

HTML Header

Lines 1 to 8 - HTML Header

Komodo Tip: Notice that syntax elements are displayed in different colors. Adjust the display options for language elements in the Preferences
dialog box.

PHP Declaration and Datafile

Line 9 - PHP Declaration

Lines 10 to 18 - Comments

Line 22 - Datafile

Komodo Tip: On line 23, type $da. Komodo displays a list of the variables declared above the cursor position that begin with the letters da. This is AutoComplete.

GuestBook Class

Lines 25 to 28 - Class Declaration

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.

GuestBook Function

Lines 34 to 37 - GuestBook Function

Komodo Tip: On line 38, 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 40 to 44 - Check for Valid Form Entry

Lines 45 to 46 - Check for Variable Value

_getData Function

Lines 53 to 58 - _getData Function

PHP Pointer: Use the "@" operator with care; you could disable error messages for critical errors that terminate the execution of the script.

outputData Function

Lines 64 to 66 - outputData Function

_createEntryHTML Function

Lines 72 to 77 - Retrieve Form Data

Lines 80 to 83 - Validate Form Data

PHP Pointer: PHP has two "or" operators: the word "or", and the symbol ||. The || operator has precedence over the word "or", providing flexibility in logic tests.

Line 86 - Current Date and Time

Lines 89 to 94 - Interpolate Form Data with HTML

_writeDataFile Function

Lines 100 to 106 - Open the Data File

Lines 108 to 110 - Write to the Data Files

Lines 111 to 113 - Close the Data File

Komodo Tip: Click on the minus symbol to the left of line 100. The entire_writeDataFile function collapses. This is Code Folding.

addGuestBookEntry Function

Lines 120 to 125 - Call Functions for Writing Data

outputForm Function

Lines 127 to 142 - The Function for HTML Form

Closing Tags

Lines 148 to 151 - Closing Tags

Running the Program

This section reviews how to run the guestbook.php program using the Komodo debugger.

  1. Run the debugger: Select Debug|Go/Continue.
  2. Configure debugging options: In the Debugging Options dialog box, configure the following options:
  3. Run the debugger: Click OK to run the debugger with the selected options.
  4. View the Command Output tab: Notice the messages in the bottom left corner of the Komodo screen; these inform you of the status of the debugger.
  5. View the rendered HTML: Click the HTML tab on the right side of the Debug tab. The HTML is displayed in the Bottom Pane; previous guestbook entries are displayed at the top of the output, and the HTML form is displayed at the bottom. Click the Output tab to return to the HTML code.
  6. Create New File: To create a new HTML file that contains the HTML code on the Output tab, select File|New|New File. In the New File dialog box, select the Common Category, and the HTML template. Click Open.
  7. Save the Output: Delete the contents of the new HTML file tab in the Editor Pane, then select the HTML code on the Output tab. Copy the contents to the new HTML file tab in the Editor Pane using the key binding associated with your selected scheme. Select File|Save As to save the file with a unique name.

Debugging the Program

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).

  1. Set breakpoints: On the guestbook.php tab in the editor, click on the gray margin immediately to the left of the code in line 9 of the program. This sets a breakpoint, indicated by a red circle. Set a second breakpoint on line 148.
  2. Run the debugger: Select Go|Continue. In the Debugging Options dialog box, click OK to accept the defaults (assuming that you created the CGI variables in the previous step, Running the Program).

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.


  1. Watch the debug process: A yellow arrow on the breakpoint indicates the position at which the debugger has halted.
  2. Line 9: Step In: Select Debug|Step In. "Step In" is a debugger command that causes the debugger to execute the current line and then stop at the next processing line (line 19). The lines between line 9 and line 19 are comments, not processing statements, and are therefore ignored by the debugger.
  3. View Variables: Expand the Bottom Pane (if necessary) by clicking and dragging the bottom margin of the Komodo workspace. Variables defined in the program are displayed on the Locals tab.
  4. Line 19: Select Go|Continue. The debugger moves to line 148. The GuestBook class is called from line 148.
  5. Line 148: Step In: The debugger is now processing the GuestBook function.
  6. View Variables: The Locals tab displays all variables.
  7. Line 35: Step In: Expand the $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.
  8. Line 36: Step In: Continue to step in until the debugger stops at the _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.
  9. Line 58: Step Out: On line 58, select Step Out to process the rest of the _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?

  • Step In: Executes the current line of code and pauses at the following line.
  • Step Over: Executes the current line of code. If the line of code calls a function or method, the function or method is executed in the background and the debugger pauses at the line that follows the original line.
  • Step Out: Executes the code without stepping through the code line by line (when the debugger is within a function or method). The debugger stops on the line of code following the function or method call in the calling program.
  1. Line 40: Step In: Continue to select Step In until the debugger is on line 121, in the addGuestBookEntry function. On line 121, the addGuestBookEntry function calls the _createEntryHTML function.
  2. Line 121: Step In: In the _createEntryHTML function, the program assigns variables to the CGI input data configured in the Debugging Options.
  3. Line 74: Step Out: The _createEntryHTML function completes, and processing returns to line 122.
  4. Line 122: Step In: Use Step In to process each line of the addGuestBookEntry function, until processing moves to the _writeDataFile function on line 102.
  5. Line 102: Step In: Process line 102.
  6. Open Watch Window: On line 102, the program opened the datafile (by default, \tmp\guestbook.dat). To watch the activity in the datafile, select Tools|Watch File, then specify the datafile.
  7. Line 103: Step In: Continue to select Step In until line 108 has been processed. After line 108 is processed, the contents of the $this->data variable are written to the datafile, as displayed in the Watch tab.
  8. Line 111: Step In: Step In until processing returns to line 45 of the GuestBook function.
  9. Line 45: Step Over: The Step Over debugger command executes the current line, including any functions called by the current line. When the debugger returns to line 46, notice that the contents of the $this->data variable have been written to the Bottom Pane.
  10. Line 46: Step Over: The debugger executes the outputForm function, which writes the HTML form to the Bottom Pane.
  11. Continue: Select Debug|Continue to run the debugger to the end of the program.

More PHP Resources

ASPN, the ActiveState Programmer Network

ASPN, the ActiveState Programmer Network, provides resources for PHP programmers:

Tutorials and Reference Sites

There are many PHP tutorials and beginner PHP sites on the Internet, including: