Reference - What does this error mean in PHP?
When debugging a PHP application, you will inevitably encounter messages like Warning: headers already sent or Fatal error: Allowed memory size exhausted. While Stack Overflow is an excellent resource, its answers often focus on the specific line of code causing the crash in one user's project. This makes them difficult for others to read because they lack context about why the error occurred.
This article consolidates those frequent errors into a single reference guide. Instead of copy-pasting fixes for your specific file, we will explain the root cause and the general solution so you can apply it to any scenario.
Runtime Logic Errors: The "Headers Already Sent" Warning
One of the most common runtime issues is Warning: Cannot modify header information - headers already sent. This occurs because PHP sends HTTP headers (like redirects or cookies) before any output is generated. If your script outputs even a single characterâsuch as a BOM, whitespace in an included file, or a stray echo statementâyou lose the ability to send headers.
The Fix:
Ensure no characters appear before the opening <?php tag. Use ob_clean() and ob_implicit_flush(false) if you must manipulate output buffers, but the best approach is often setting display_errors = Off in your production php.ini or using a reverse proxy to hide raw errors while fixing the logic.
Database and Stream Warnings
Messages like Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given indicate that a database query failed silently before being passed to a fetch function. The mysql_* extension is deprecated and removed in PHP 7.0+. If you are using this error today, you must migrate to PDO or MySQLi immediately.
Similarly, Warning: open_basedir restriction in effect means your code is trying to access a file outside the directories allowed by the server configuration. This is a security feature; you cannot fix it by changing code aloneâyou must adjust your hosting environment's php.ini.
Parse Errors: Syntax and Structure
Parse errors happen before execution starts. They indicate that the PHP interpreter cannot read your file structure.
- Unexpected '[': You likely have an opening bracket without a matching closing one, or you are using
[in a context where it isn't valid (e.g., inside a class definition body). - T_PAAMAYIM_NEKUDOTAYIM: This refers to the
::operator. If you see this error, you might be using static properties incorrectly, such as trying to access a non-existent static variable before defining it. - Unexpected 'require_once': This often happens when a file is included inside a function or class method in older PHP versions, or if the syntax for namespaces is malformed.
Fatal Errors: Execution Halted
Fatal errors stop script execution immediately.
- Memory Exhausted: Your script requested more memory than the
memory_limitdirective allows. You can increase this limit via.htaccessorphp.ini, but optimizing your code to release resources (closing DB connections, freeing objects) is the sustainable solution. - Call to member/function on non-object: This means you are trying to use an object that was never instantiated or has been garbage collected. Check your variable scope and initialization logic.
- Undefined Class Constant/Function: Ensure you haven't misspelled a constant name or failed to
usethe correct namespace for a function. - Return type incompatibility: With PHP 8+, if a parent class defines a method with a return type (e.g.,
string) and the child overrides it withvoidor no type, you will get a fatal error unless you add the#[\ReturnTypeWillChange]attribute.
Notices vs. Warnings: Silent Data Corruption
Notices and warnings do not stop execution but often indicate logic bugs that corrupt your data flow.
- Undefined Index/Array Key: You are accessing
$array['key']when the key does not exist. In modern PHP, suppress these with null coalescing (??) or ensure defaults are set before access. - Array to string conversion: Attempting to echo an array directly prints its internal representation (e.g.,
Array). This usually indicates a logic error where you expected a scalar value. Note that this is aWarningin PHP 8.0+ and aNoticein earlier versions. - Property of non-object: Similar to the member function error, this happens when trying to access
$object->propertyon a variable that is not an object instance.
SQL Syntax Errors
The message You have an error in your SQL syntax usually points to a typo in a query string or a missing closing parenthesis. It also suggests that the query might be running against a different MySQL version than expected, requiring manual adjustment of the query logic.
Debugging Strategy
When facing these errors, do not just search for the exact message text. Identify the category (Syntax, Runtime, Logic) and check your environment configuration. If you are using Laravel or similar frameworks, ensure that error reporting is set to debug in development (APP_DEBUG=true) to get detailed stack traces pointing directly to the problematic file and line number.
