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");
Call
What it does
error_log("Something happened")
Sends the message to the default log, which is set by the error_log setting in php.ini.
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.
Optional. 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.
destination
Optional. 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_headers
Optional. Extra email headers. Only used with message type 1.
Return Value
Return Value
Description
true
Returned when the message was logged successfully.
false
Returned 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.
ini_set() points the error_log setting to our temporary file.
error_log() with only a message uses type 0, so the text goes to that file.
PHP adds a timestamp in square brackets at the start of the line, such as [11-Nov-2026 10:00:00 UTC].
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.