Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

error_log

Writes a message to the error log.

Syntax

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

Parameters

ParameterTypeDescription
$messagestringThe message text.
$message_typeintOptional. `3` writes to the file given as the third argument.
$destination?stringOptional. The path to a log file.

Return value

bool`true` if the message was written.

Examples

Writing to a log file of your own:

<?php
 
$log = sys_get_temp_dir() . '/ref-app.log';
file_put_contents($log, '');
 
error_log("Payment failed: order 15\n", 3, $log);
 
echo file_get_contents($log);

Output:

Payment failed: order 15

Notes

Without the second and third arguments the message goes to the server log — the user does not see it. Do not log passwords or card numbers.

See also

  • var_dump — Prints the type and the value — for debugging.
  • set_error_handler — Sets a custom function to handle PHP warnings and notices.

Related recipes

In the tutorial

  • Errors and debugging — The kinds of PHP errors, error display settings, the log, turning warnings into exceptions and debugging techniques.

Thanks for reading!

Donate to the site — we work hard for you.