How to properly print variable to PHP error log

How to properly print variable to PHP error log

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why error_log(print_r($var)) Often Fails in PHP

If you are debugging a script and need to inspect data stored in memory, the instinctive move is often to dump the variable into the server's error log. You might set error_reporting(E_ALL) and log_errors to true, configure a specific file path like /var/log/errorlog.log, and then attempt to log the output using error_log(print_r($results)).

In many scenarios, this approach yields silence. The log file remains unchanged, leaving you with no data to analyze. This happens because of how PHP handles logging internally versus how it handles output buffering and formatting.

The Core Issue: String Conversion vs. Log Entry

When you call error_log(print_r($results)), you are passing a string to the function. However, the effectiveness of this call depends heavily on the state of your configuration and the content being logged.

The most common reason for silence is not that the variable isn't being passed, but rather that the log stream is not configured to write to the specific file path you expect, or the web server (Apache/Nginx) and PHP-FPM processes are running in different contexts. If your php.ini file sets error_log to a relative path, it might resolve differently depending on where the script runs. If it points to an absolute path like /errorlog.log, ensure that directory exists, is writable by the web server user (e.g., www-data), and that no permissions block the write operation.

Furthermore, there is a subtle behavior regarding what gets logged. The error_log() function writes to the error log stream defined by ini_set("error_log", ...). If you are running PHP via a web server like Apache or Nginx with PHP-FPM, the error log is often configured globally in the main php.ini or virtual host configuration, not dynamically overridable by a simple ini_set() call within a script. Changing the setting in code works for CLI scripts, but rarely overrides the server's persistent configuration for web requests.

Formatting and Data Integrity

Even if the log writes successfully, simply wrapping your variable in print_r() inside the string argument can lead to parsing issues later on. The error_log function accepts a message string and an optional severity level. When you do:

error_log(print_r($results));

You are logging a formatted string representation of the array. This is generally safe, but if $results contains very large objects or complex nested structures, the output can become massive, potentially hitting buffer limits or making the log file unreadable for quick inspection.

A more robust approach often involves using var_export() instead of print_r(). While both produce readable output, var_export() generates valid PHP code that represents the data exactly. If you later decide to re-import the logged data into a script (e.g., for unit testing or replaying state), var_export() is superior because it preserves class information and properties accurately, whereas print_r() often truncates object details in verbose modes depending on configuration.

Handling Large Datasets and Query Results

In your specific case, you are fetching up to 500 records from a MySQL database. Logging the entire result set of 500 rows into an error log is a significant anti-pattern for production environments. It clutters the logs with data that serves no diagnostic purpose once the request completes, making it hard to spot actual errors among the noise.

If you must debug the query results, consider logging only the count or the first few records during development:

$debug_log = "Query returned " . count($results) . " rows.\n";
if (!empty($results)) {
    $debug_log .= "First record: " . print_r($results[0], true);
}
error_log($debug_log);

This keeps the log file manageable and focused on exceptions rather than expected data volume. In production, you should disable log_errors or ensure your logging strategy is strict about what constitutes an error versus debug output. Tools like Monolog are often preferred over raw error_log() calls because they offer structured formatting, rotating logs, and distinct channels for different types of messages, preventing your error log from becoming a "fat" file filled with JSON dumps of normal data.

Conclusion on Debugging Strategy

The takeaway is that configuring ini_set in code rarely overrides server-level logging directives. Always verify the actual path in your server's configuration files. Additionally, be mindful of logging entire datasets; use conditional checks to log only when an error occurs or for limited debugging sessions. For complex data structures, prefer var_export() if you need to preserve type information, but keep the verbosity low in production logs to maintain performance and readability.

Our network