PHP Function Reference

PHP error_log() Function



The PHP error_log() function is used to send an error message to the web server's error log or to a file.

Syntax

error_log(message, message_type, destination, extra_headers)

Parameters

message Required. Specify the error message that should be logged.
message_type Optional. Specify where the error should go. The possible message types are as follows:
  • 0 - (Default) Message is sent to PHP's system logger, using the Operating System's system logging mechanism or a file, depending on what the error_log configuration directive is set to in php.ini.
  • 1 - Message is sent by email to the address in the destination parameter.
  • 2 - No longer an option (only available in PHP 3).
  • 3 - Message is appended to the file destination.
  • 4 - Message is sent directly to the SAPI logging handler.
destination Required. Specify the destination. Its meaning depends on the message_type parameter as described above.
extra_headers Required. Specify the extra headers. It is used when the message_type parameter is set to 1. This message type uses the same internal function as mail() does.

Return Value

Returns true on success or false on failure. If message_type is zero, this function always returns true, regardless of whether the error could be logged or not.

Example: error_log() examples

The example below shows the usage of error_log() function.

<?php
//send notification through the server log 
//if error connecting to the database
if (!mysqli_connect("localhost", "username", "password","my_db")) {
  error_log("Failed to connect to database!", 0);
}

//notify admin by email if run out of FOO
if (!($foo = allocate_new_foo())) {
  error_log("Trouble, we are all out of FOOs!",
                1, "admin@example.com");
}

//another way to call error_log():
error_log("You messed up!", 3, "./my-errors.log");
?>

❮ PHP Error Handling Reference