3 Most Common PHP Errors You Should Know To Fix

There were times in my coding life where I’ve wasted a great deal of time to find a solution for some error when I could have easily figured it out myself if I looked closely at the code. But I know eyes can be deceiving after spending hours and hours on the same code, so minute errors would be the last to notice.

You must have been there and done that too. So here I will be listing down 3 most common errors (and warnings) found in PHP and possible fixes so that you won’t have to spend even a minute scratching your head and ‘googling’ for solutions!

3-most-common-php-errors-you-should-know-to-fix-4892987

#1 : Headers Already Sent

Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at /home/notes99/public_html/pablo.lk/dev/phperrors.php:2)

Warning: Cannot modify header information – headers already sent by (output started at /home/notes99/public_html/pablo.lk/dev/phperrors.php:1)

This is actually a warning rather than an error which is mostly encountered with session_start() and header() functions.

And those who don’t know the solution might be amazed to hear that it can occur with just a misplaced white space!

Yes, if you come across this the next time check your code immediately whether there are any white spaces, empty lines or any form output is sent before the ‘

Correct:


#2 : Session Variable Not Accessible

session-variable-not-accessible-8516063

If you are dealing with session variables you must have come across with this error at least once in your life time.

Simply this ‘Undefined variable’ error occurs when you are trying to access a session variable without calling session_start() . So whenever you are accessing sessions, make sure you place session_start() as the first thing in your php script.

#3 : Unexpected T_ error

unexpected-t_-error-4565265

This is a very common syntax error that frequents in PHP coding.

When you come across such error, all you have to do is to check for a missing semi-colon, quotation mark or a parenthesis in the line number mentioned or immediately above that.

Also be thorough with the opening and closing of blocks (i.e. if’s, while’s, for’s)

It’s as simple as that!

My objective was to give you a basic understanding of these common errors/warnings so that you can identify and fix them as soon as you spot without wasting your valuable time searching for solutions. If you have any more suggestions for common errors do share with us here.

Tags: #PHP