Common PHP Problems

Common PHP Problems

A few problems crop up quite regularly among our forum users; In order to help speed things along, see if your problem is listed below before posting a thread on the forum. Please note that the causes listed are the most common causes, and are not the only causes for the problems listed.

1. The White Page of Syntax Errors (WPSE, or “Whoops”)
Symptom: A blank page appears rather than any of your pretty layout or other elements.
Cause: Fatal Error preventing display.
Solution: First, add these lines to the top of your code:


Run the page again. An error should now be displayed pointing to the cause of your problem. More often than not this is caused by a missed semicolon or other closing punctuation (parenthesis, brace, quotation mark, etc).
At the very least, this information will help us narrow down your problem if you still cant see one.

2. Unexpected T_<SOMETHING>, Unexpected $end, and other Unexpecteds
Symptom: Error message generated says Unexpected T_<something>, $end, or a punctuation mark.
Cause: Missed a closing punctuation prior to this point.
Solution: Look at the listed error line and the 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 (things surrounded by curly braces, used in [FPHP]if[/FPHP], [FPHP]while[/FPHP], [FPHP]foreach[/FPHP], [FPHP]for[/FPHP]…).

3. My database query doesn’t work.
Symptom: Unexpected results or non-execution of a SQL (mySQL,msSQL,PDO,etc) query.
Cause: Varied
Solution: First and foremost, 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 ([FPHP]mysql_error[/FPHP], [FPHP]mysqli_error[/FPHP] etc) such as:

$sql = "SELECT fail FRAM failtable WHERE id = $id";
echo $sql;
echo mysql_error();

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. My [FPHP]IF[/FPHP] isnt doing what it’s supposed to.
Symptom: [FPHP]IF[/FPHP] statement seems to be always executing or changing the variable.
Cause: Declaration vs. Comparison on Equals To condition.
Solution: Check to make sure you are using == instead of = in your comparison.

5. Headers already sent (Also applies to [FPHP]session_start[/FPHP] failure)
Symptom: Error message declares headers already sent, when attempting to use the [FPHP]header[/FPHP] or [FPHP]session_start[/FPHP] function.
Cause: Whitespace at top of script.
Solution: 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 [FPHP]header[/FPHP] function can only be used once per execution.

6. Undefined index / Undefined offset
Symptom: One of two error messages thrown: Undefined index or Undefined offset.
Cause: Referencing an array element that does not exist. Index implies an Associative array reference, Offset implies a Numerical array reference.
Solution: Determine why the value you are searching for does not exist - Misspelt, value unpassed due to malformed <FORM>, [FPHP]for[/FPHP] loop has the wrong end condition are all common causes.

7. Warning: functionname() expects parameter X to be <a type>, <a different type> given
Symptom: Calling a function (most commonly a database function such as [FPHP]mysql_fetch_assoc[/FPHP]) results in warning error.
Cause: Input of unexpected type.
Solution: If this is a database function, check the solution listed in #3. Your previous database function is returning FALSE, which indicates a problem in that function.
If it’s not a database function, check the input you are giving to the function for it’s type. [FPHP]var_dump[/FPHP] the variable if necessary, as it will display the type.

8. Session variables disappearing/unaccessable
Symptom: Attempting to access elements of the $_SESSION array result in undefined index errors.
Cause: Lack of [FPHP]session_start[/FPHP]
Solution: Be certain that [FPHP]session_start[/FPHP] is called at the beginning of every page. Without it, PHP does not retrieve the session variables.

9. “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.”
Symptom: Using any of PHP’s date or time functions generates the above warning, or something similar.
Cause: Improperly defined timezone in php.ini
Solution: Either adjust your php.ini file to set the date.timezone setting, or call the [FPHP]date_default_timezone_set[/FPHP] function at the beginning of your script to identify the timezone.

My problem is listed, but the solution didn’t work!
Then do a Search on the forum for posts similar to your own; if you do not find one that matches, feel free to create a new thread, and mention in your post that you have already attempted the solution above, to prevent repetition.