PHP Phreakout: Troubleshooting Tips for PHP Code

By: Steven Seiller

Page 3 of 4

Set for printing

Previous Next

Spell Checker for PHP - The best way to deal with syntax problems is to prevent them! Before trying your PHP code, use a syntax checker to ensure it conforms to PHP standards for grammar and punctuation. PHP has a built-in syntax checker which scans your file for parse errors. To use this feature, you need to feed the following to the command line:

php -l /path/to/your/file.php

If you are not comfortable with the command line or don't have access to it on your web server, there are some other options. I use a process described by John Gruber on my Mac which uses Applescript to submit my BBEdit file to the command line and report the results.

Figure 1 PHP Syntax check Error Message

Other editors offer this feature, but I have not found such a solution for Dreamweaver. You might try a web site which offers this function as a free service.

Logical Error: An Oxymoron? - Logical errors can be difficult to troubleshoot since you will typically not receive a specific error. Logic errors occur when your code doesn't fail from technical problems but does not give you the result you expected. Many times, the result you get is no result at all which gives even less to work with.

In my experience, I create logic errors when I:

  • use the wrong variable (altogether or because of a typo)
  • use the wrong comparison operator to trigger an operation
  • assume I have a specific result from a previous operation
  • assume I have a connection to a remote file
  • process data in a way I did not intend

Breadcrumbs To Sanity - The way I track down logic problems is to backtrack through my code to confirm the result I am expecting is the result that I am receiving. This I do primarily by displaying on screen the contents of variables and the results of operations not normally shown. To use these methods, I insert them as needed in the flow of the code and run the script to see the output. Once I have solved the problem, I removed the testing code from the script to clean up the output. The following are functions I use for this purpose of testing.

echo - Echo the value of the variable on-screen to verify its value. I echo the name of the variable with its value to not only make clear which variable is being outputted, but also to mark the spot of output in case there is no value to show. If I didn't print the name of the value, nothing might be printed which does not help clarify the process.

echo "myVar: " . $myVar . "<br>\n";


myVar: 18

print_r() - Print_r shows the value of a variable in a human readable format. This function is particularly useful for arrays. Note the use of the <pre> tags when ensures that your web browser will keep the output readable. This method is especially handy for verifying the data returned by your web form which would check the $_POST array.

echo "<pre>";
print_r ($myArray);
echo "</pre>";


  [a] => apple
  [b] => banana
  [c] => Array
    [0] => x
    [1] => y
    [2] => z

var_dump() - For more complex scripts which manage a lot of environmental data, you can use the var_dump() function to print all defined variables. Be forewarned that you will likely receive dozens of screens full of data, but if you are looking for that proverbial needle in the hay stack this is the equivalent of using a leaf blower! If you are just learning PHP to use as a Common Gateway Interface (CGI), the output of this function will show you an amazing amount of resources at your disposal.

echo "<pre>";
echo "</pre>";

assert() - Sometimes when you don't get the result you are expecting it is difficult to tell if a control structure has been processed as expected resulting in the non-response or if it didn't process properly resulting in the unexpected result. The assert() function will show a warning when the statement being asserted is false. I think of this function as a checkpoint in the PHP code flow.

56 //-----------------TESTING-----------------//
57 assert(false);
58 //-----------------TESTING-----------------//


Warning: assert(): Assertion failed in /home/site/public_html/test.php on line 57

The usefulness of this function is shown in the following exercise.

15 $action = "Save";
33 if ($action = "Lookup") {
34   // Start database lookup procedure...
35   echo "Loading..."
36   ...
37 } else {
38   // $action = Save so start database Save procedure...
39   assert(false);
40   ...



In this example, even though I verified the $action variable is set to "Save" before line 33, the save process starting on line 40 is not happening. To confirm whether the problem was with my database save procedure I included the assert function at line 39. What is supposed to happen is that once the script sees on line 37 that the $action variable is set to "Save", it will display the assert warning. The warning did not appear.

Quick Quiz: Do you know why the assert warning did not appear? Go to the article support forum using the link at the conclusion of this article. Earn a gold start by submitting your guess on the error.

Page 3 of 4 Previous 1 2 3 4 Next

Download Support Files

Downloads are disabled during your trial period.

PHP, Code, Coding, Error, Troubleshooting, Fatal, Notice, Warning, Parse, Syntax, Semantic, Environment, Logic