Simple techniques and concepts for debugging php code when getting blank pages or errors. Includes descriptions of common errors and their causes.
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.
To turn php errors on, add this code to the top of your page:
<!--?php ini_set('display_errors',0); ?-->
Know the three debugging statements.The three statements used are:
var_dump(); //takes an array or object of any type and tells you all about that object.
echo(); //writes a string to the page.
die(); //stops the code from processing.
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.
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.
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.
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'); ?>)
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.
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.
Text to come
Text to come
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.
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.
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.
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 commentsYour friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.
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.