PHP error_log() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Error

Definition

In PHP, error_log() sends a message to a log. Depending on the settings, the log can be the server error log, a file you choose, or even an email.

Instead of printing debug messages on the page where visitors can see them, you can write them to a log and read them later. The most beginner-friendly option is message type 3, which appends your message to a file of your choice.

Syntax

Call error_log() with the values you need. Start with this line.

PHP
error_log("Something happened");
CallWhat it does
error_log("Something happened")Sends the message to the default log, which is set by the error_log setting in php.ini.
error_log("Saved" . PHP_EOL, 3, "/tmp/app.log")Appends the message to the file /tmp/app.log.
error_log("Alert", 1, "[email protected]")Sends the message by email to the given address.

With message type 3, PHP does not add a new line for you, so end each message with PHP_EOL. With type 0, PHP adds a timestamp when it writes to a log file.

Library declarations
PHP
error_log(string $message, int $message_type = 0, ?string $destination = null, ?string $additional_headers = null): bool

Parameters

ParameterDescription
messageRequired. The text to log.
message_typeOptional. Where the message goes. 0 is the system log or the file set in the error_log setting (the default). 1 sends an email. 3 appends to a file. 4 sends the message straight to the SAPI logger.
destinationOptional. Depends on the type. For type 1 it is the email address. For type 3 it is the file path. It is ignored for types 0 and 4.
additional_headersOptional. Extra email headers. Only used with message type 1.

Return Value

Return ValueDescription
trueReturned when the message was logged successfully.
falseReturned when logging failed, for example when the file cannot be written.

PHP Examples

Example 1 — Writing a Message to a File

Message type 3 appends text to a file. We then read the file back to prove that it worked.

PHP
<?php
$file = sys_get_temp_dir() . '/codetofun-app.log';

if (file_exists($file)) {
    unlink($file);
}

$ok = error_log("User logged in" . PHP_EOL, 3, $file);

var_dump($ok);
echo file_get_contents($file);

unlink($file);

How It Works

  1. sys_get_temp_dir() gives a folder that PHP can normally write to, and we add a file name to it.
  2. error_log() with type 3 appends the message to that file. We end the message with PHP_EOL for a new line.
  3. The function returns true, so var_dump() prints bool(true).
  4. file_get_contents() reads the file back, and unlink() deletes the demo file.

Example 2 — A Small Logging Helper

Wrap error_log() in your own function to add a level label to each line.

PHP
<?php
$file = sys_get_temp_dir() . '/codetofun-levels.log';

if (file_exists($file)) {
    unlink($file);
}

function logMessage(string $level, string $text, string $file): void {
    error_log("[" . $level . "] " . $text . PHP_EOL, 3, $file);
}

logMessage('INFO', 'Started', $file);
logMessage('WARNING', 'Disk low', $file);
logMessage('ERROR', 'Save failed', $file);

$lines = file($file, FILE_IGNORE_NEW_LINES);

echo "Lines logged: ", count($lines), PHP_EOL;
foreach ($lines as $line) {
    echo $line, PHP_EOL;
}

unlink($file);

How It Works

  1. logMessage() builds a line such as [INFO] Started and ends it with a new line.
  2. Each call appends to the same file, so the three lines stack up in order.
  3. file() reads the file into an array with one item per line, and FILE_IGNORE_NEW_LINES removes the line endings.
  4. We print the number of lines and then each line, and finally delete the demo file.

Example 3 — Using the error_log Setting

With the default message type 0, PHP writes to the file named in the error_log setting and adds a timestamp.

PHP
<?php
$file = sys_get_temp_dir() . '/codetofun-default.log';

if (file_exists($file)) {
    unlink($file);
}

ini_set('error_log', $file);

error_log("Something happened");

$content = file_get_contents($file);

echo "Log has message: ", str_contains($content, 'Something happened') ? 'yes' : 'no', PHP_EOL;
echo "Starts with bracket: ", $content[0] === '[' ? 'yes' : 'no', PHP_EOL;

unlink($file);

How It Works

  1. ini_set() points the error_log setting to our temporary file.
  2. error_log() with only a message uses type 0, so the text goes to that file.
  3. PHP adds a timestamp in square brackets at the start of the line, such as [11-Nov-2026 10:00:00 UTC].
  4. That is why the file contains our message and its first character is an opening bracket.

Frequently Asked Questions

It sends a message to a log. By default it goes to the server's error log, but you can send it to a file of your choice or by email.
Use message type 3 and give the file path as the destination, like error_log('Message' . PHP_EOL, 3, '/path/to/app.log'). PHP appends the text to the end of the file.
With message type 3, PHP writes exactly what you give it. Add PHP_EOL to the end of your message to start a new line.
It uses the error_log setting from php.ini. If that setting is empty, the message goes to the web server's error log, or to the terminal's error stream when you run PHP from the command line.
It returns true when the message was logged and false when it failed, for example because the file could not be written.

Next: error_reporting()

error_reporting() sets which kinds of errors PHP should report, and it returns the previou.

error_reporting() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful