Dealing With Common PHP Errors

Jan 27, 2014, by admin

Working with PHP, we often face few common problem and errors related to PHP script such as white page of syntax error, unexpected errors, header already sent, Undefined index or Undefined offset, System time zone issue etc. In this article I have listed the most common PHP error or  problems when coding with PHP. With each problem, a brief solution suggested with the error Symptom and causes. Hope this will work for you.

php_logo

Headers already sent
The possible error message “Error message declares headers already sent, when attempting to use the header or session_start function“. These type errors mostly occurred due to whitespace at top of script or sometimes with session start. Always make sure that the < of your first <?php is line 1, character 1. There can be no whitespace, no blank line, anything, prior to that PHP block opening, or the HTML headers are considered sent. The header function can only be used once per execution.

Undefined index or Undefined offset
We often face error says “Undefined index”  or “Undefined offset“. This type of error caused when referencing an array element that does not exist. Index implies an Associative array reference; Offset implies a Numerical array reference. To fix this issue, determine why the value you are searching for does not exist – Misspelt, value unpassed due to malformed <FORM>, for loop has the wrong end condition are all common causes.

System timezone issue
This type of php error  “It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function.” occurred due to improperly defined timezone in php.ini. When we use any of PHP’s date or time functions, we get above error message or something similar. To fix this issue, either adjust your php.ini file to set the date.timezone setting, or call the date_default_timezone_set function at the beginning of your script to identify the timezone.

Session variables disappearing/unaccessable

Error occurred when attempting to access elements of the $_SESSION array result in undefined index errors. This error caused when we missed session_start . Always be careful to inititialze session_start at beginning of your script. Without it, PHP does not retrieve the session variables.