Common PHP Errors & Fixes

Oct 17, 2013, 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.

1. The White Page of Syntax Errors ( Whoops etc )

This type of error is called fatal errors in PHP which prevent display page. Only a blank page appears rather than any of your pretty layout or other elements. To display errors in a page add below line at top of your php script.

  1. <?php
  2. ini_set(‘display_errors’,1);
  3. error_reporting(E_ALL);
  4. ?>

After you run the page again, an error should now be displayed pointing to the cause of your problem. This error mostly occurred due to missed semicolon or other closing punctuation such as (parenthesis, brace, quotation mark, etc).

2. Unexpected T_, Unexpected $end, and other Unexpected

This generated error message says “Unexpected T_, $end, or a punctuation mark“, is generated when we missed a closing punctuation prior to this point.
You can find this error at listed error line and line directly before. Most likely you missed a semicolon, quotation, or closing parenthesis on those lines. If the error says Unexpected $end , you’ve forgotten to close one of your statement blocks (Mostly curly braces, used in if, while, foreach, for).

3. My database query doesn’t work

Mostly the error message says “ Unexpected results or non-execution of a SQL (mySQL,msSQL,PDO,etc) query.” To find the issue, first echo out your query to see if it is actually getting the variables you believe it should be. Also make sure you are echoing the error function of your given database type. If so, run the query on your database using your database tool of choice, or echo the error capturing functions of your chosen database connection type (mysql_error, mysqli_error etc) such as:

  1. <?php
  2. $sql = “SELECT fields FRAM table WHERE id = $id”;
  3. echo $sql;
  4. mysql_query();
  5. echo mysql_error();
  6. ?>

After running above query, if you receive a Warning about a function expecting a parameter to be a resource, check the function previous to the one that generated the error, as it has generated errors. If the results of said query are not what you were expecting, see the Databases forum (or MySQL subforum) for assistance as to your query.

4. 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.

5. 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.

6. 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.

7. 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.