close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Web development tutorial

Debugging PHP Scripts

Tutorial created by Jared Lui, Synectx Web Design/Development

Categories: PHP

rating

Simple techniques and concepts for debugging php code when getting blank pages or errors. Includes descriptions of common errors and their causes.

arrow downUnderstanding PHP Debugging

This section briefly describes the 4 most important snippets of code used in debugging PHP and several concepts needed to use them correctly.

In order to fix any issue in your code you must first locate it. This is the main objective when using these tools. The following should give you an understanding of what these snippets of code do and how to use them to your advantage.
Turn on error messages for your pageSometimes rather than an error message you'll get a blank page instead of the expected results. This happens for one of two reasons. Either there is a syntax error that is causing it so it can't show you the message which sometimes happens OR you have error messages turned off.

generally, php errors is turned off by default. The reason it is suggested that you turn off php errors is in case errors occur, you do not give too much information about your code to the general public. However, it's useful to have on when building or debugging your site. However, some servers don't allow you to turn on/off server errors.

To turn php errors on, add this code to the top of your page:

php:
<!--?php ini_set('display_errors',0); ?-->


Know the three debugging statements.The three statements used are:

php:
var_dump();  //takes an array or object of any type and tells you all about that object.
php:
echo();  //writes a string to the page.
php:
die();  //stops the code from processing.

arrow downUsing die(); Statements

Remember, the objective of debugging is to locate the first line of code causing the problem. Once you fix that problem you can move on to the next bug if present.

In cases where you get blank pages instead of error messages you have to locate the error(s) manually. Knowing that code executes from the top down you can use die statements to stop the code at certain points to see if the code is failing above or below it. Because there could be multiple lines with errors, it's a good idea to start at the top and work your way down as in the following example.

Step 1: Discover an error

Below, the browser output displays a blank page. This is not the desired result however no error message is displayed and we are unable to display error messages on our server.

Step 2: Add your die statement

In Step 2 below, begin by using a die statement at the top of the page and include a word to display that shows the code is good to this point. (<!--?php die('working1'); ?-->)
Run this page in your browser and you should see the word displayed on the page. This indicates that everything above this die statement is working.

Step 3: Begin moving down the page

Now move the die statement down the page to the next set of executable code and edit the word. Using our example in Step 3, change working1 to working2. (<?php die('working2'); ?>)

NOTE: The reason we do this is so that when we rerun the code in the browser, we can tell that our code is in fact working and not simply cached in the browser.

Our browser output displays "working2" so this tells us that all of the code above the die statement is working correctly (lines 1-4 in the code).
The idea is to keep moving the die statement slowly down the page until you get to a point where your word does not display.

Step 4: Found an error

After moving the die statement ( <!--?php die('working3'); ?-->) further down the code as seen in Step 4 and running the code in the browser, the page is blank like it was in Step 1 above. This tells us that there is an error in the code above the die statement. We already know the code is fine on lines 1-4. Therefore the code is broken somewhere between lines 5-12.

You can narrow the search down by inserting the die statement between lines 8 and 9 to determine if it is above or below. You could also work your way up from 12 until you find the bad code.



Once you find the error and correct it, running the page again in the browser should display the expected results as in Step 5.

arrow downUsing var_dump(); Statements

Text to come

arrow downUsing echo(); Statements

Text to come

arrow downCommon Errors - Diagnosis and Fixes

Every web designer/developer has run into these at one time or another and after you have fixed them a time or two will know to look for them in advance or will at least begin to recognize them when they occur.

Step 1: Failed to open stream


MeaningThe file or directory that is executing the code cannot access the required file it is calling for. It gives the line number in the code where it is making the call from so that you can debug it as needed.

OccurrenceThis happens most often when wizards create additional support files that have not been uploaded. It also happens when you copy and paste code and don't change paths as needed.

The solution Upload the file that is required or correct the path to the file so the executing file can access it. In the example above, the error is telling you the filename, path and line number it is being called from. You can see that in the code below. In this case the path is wrong and should be "webassist/security_assist/wa_cryptencryption.php" without the "../" in front.

Step 2: Headers already sent


MeaningIn laymen terms it means that output to the client had begun before it was ready to do so and now it cannot send the proper beginning statement. Instead, a blank space was sent so the code below the space throws errors (which can be many if there are multiples).

OccurrenceGenerally happens when using headers() to redirect and there is a blank or space above the <!--? ~ ?--> code.

The solutionRemove any blank lines or spaces between your code. Your php code should always be tight and never have space between them. In the code below line 1 is blank. Delete the blank line and the error is corrected.

NOTE: a blank between any other <!--?php ?--> statements such as on line 5, between 7 and 8 or between 13 and 14 in the code below would also give a headers already sent error.

arrow downReviews and comments

Comments will be sent to the author of this tutorial and may not be answered immediately. For general help from WebAssist, please visit technical support.

Sign in to add comments
rating

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.