In PHP, is there a way to display human-readable information about a variable in error_log()?

StackOverflow https://stackoverflow.com/questions/17074587

  •  31-05-2022
  •  | 
  •  

سؤال

I want to do something like:

error_log('this is information about the variable: '.print_r($variable));

or

error_log('this is information about the variable: '.var_dump($variable));

FYI, the variable I'm trying to print is an array.

هل كانت مفيدة؟

المحلول

print_r() accepts a second parameter which will return its output as a string. As such, your first example can be modified to the following:

error_log('this is information about the variable: ' . print_r($variable, true));

Note: While var_dump() does not have such a parameter, you could use output buffering to store its output as described in the docs.

نصائح أخرى

error_log(
    'this is information about the variable: '.print_r($variable, true), 
    3, 
    "/var/tmp/my-errors.log");

More info: http://de3.php.net/manual/en/book.errorfunc.php

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top