PHP Common Problems
One of the most frustrating problems is the getting a page with no output at all, commonly referred to as the "White Screen of Death". While this can strike panic in beginner developers, it is best to stay calm and try to narrow the problem by doing the following:
No Output
Is the Web Server Running
The problem might be that your web server is not running. Try to access an HTML page. If we can't access the HTML page either this is likely the case. This will also help verify that we are access the right directory.
Is PHP Working
Create a simple PHP page using the phpinfo()
function. This is a simple way to check if our web server can process PHP and confirm that PHP is working.
Is Display Errors On
PHP errors occur when there is something wrong with our code. It can be something as simple as a missing semicolon or a typo in a variable name. Knowing the what type of error has occurred is vital for knowing where and how to fixe the problem. However, by default, PHP does NOT display these errors.
We will discussed how to turn on display errors on the next page. But now lets talk about common mistakes that can cause these errors.
Common PHP Mistakes
Below is a list of common mistakes that are made when working with PHP. These mistakes can happen to beginners as well as seasoned developers.
Typos
Typos happen. Unfortunately, typos can cause problems in our code. That why it is important to review our code carefully and thoroughly and pay attention to details.
Missing Semicolon
Semicolons (;
) are really important in PHP. We must be alway remember to end each line with a semicolon.
Missing closing brackets
It can be easy to forget to close a curly brace (}
), square bracket (]
) or parentheses ()
), especially when working with nested code. Fortunately, our text editor will usually help us, here by auto generating a closing bracket.
Missing closing quote
Like forgetting to close a bracket, a missing quote can also result in an error. It is important to remember that closing quote must match the opening quote, and to escape any matching quote that maybe in the string.
Misspelled variable names
Misspelling or mistyping a variable name is another common mistake. Remember that PHP is case-sensitive, so casing matters when comes to variable names as well. This is way it is important to find a naming convention and stick with it.