Var_dump to log file php

Writing objects to the PHP error log using error_log() can be useful, but it isn’t allowed – you can only write strings there. What to do?

Well, luckily there are options for both print_r() – human friendly – and var_dump() – parseable – that will let you convert an object into a string.

Use print_r()

Using print_r() is the easiest, since the second parameter to the function says that we want to send the output to a variable rather than echoing it. Thus, to write to the error_log you just need to use the following code.

$object = new MyObject();
error_log( print_r( $object, true ) );

Use var_dump()

If you want the more detailed output of var_dump(), it’s a bit trickier, but still pretty easy. In a nutshell, since you can only echo the results you have to capture the output buffer with ob_start(), assign it to a variable, and then clean the buffer with ob_end_clean() allowing you to write the resulting variable to the error_log.

function var_error_log( $object=null ){
    ob_start();                    // start buffer capture
    var_dump( $object );           // dump the values
    $contents = ob_get_contents(); // put the buffer into a variable
    ob_end_clean();                // end capture
    error_log( $contents );        // log contents of the result of var_dump( $object )
}

$object = new MyObject();
var_error_log( $object );

I have the php code for sql query

> " .$server ;
		
		$result = mysql_query('SHOW DATABASES');
        echo "
"; while ($row = mysql_fetch_array($result)) { var_dump ($row); } } } ini_set('max_execution_time', 10); return $link; ?>

this code print my database name on the browser how I can save the database name into text file

Connected successfully to >> 127.0.0.1
array(2) { [0]=> string(18) "information_schema" ["Database"]=> string(18) "information_schema" } array(2) { [0]=> string(2) "db" ["Database"]=> string(2) "db" } array(2) { [0]=> string(5) "mysql" ["Database"]=> string(5) "mysql" } array(2) { [0]=> string(10) "phpmyadmin" ["Database"]=> string(10) "phpmyadmin" } array(2) { [0]=> string(4) "test" ["Database"]=> string(4) "test" }

Var_dump to log file php

RiggsFolly

90.8k20 gold badges101 silver badges145 bronze badges

asked Aug 12, 2016 at 23:15

Var_dump to log file php

4

You can use the output buffering functions to capture output and write it to a file.

ob_flush();
ob_start();
while ($row = mysql_fetch_assoc($result)) {
    var_dump($row);
}
file_put_contents("dump.txt", ob_get_flush());

answered Aug 12, 2016 at 23:25

8

Don't use var_dump for this, use serialize like so:


To restore it, you can use unserialize($filecontents); by reading it back in from the file.

answered Aug 12, 2016 at 23:25

1

> " .$server ;

        $result = mysql_query('SHOW DATABASES');
echo "
"; while ($row = mysql_fetch_array($result)) { fwrite($rez, $row['DatabaseName']); } } } ini_set('max_execution_time', 10); return $link; ?>

answered Aug 12, 2016 at 23:29

This should work

$file = 'somefile.txt';
file_put_contents($file, $some_var);

You may want to serialize the variable first to make it more readable.

But there are several other methods: http://php.net/manual/en/function.file-put-contents.php

answered Aug 12, 2016 at 23:19

AndyAndy

1021 silver badge4 bronze badges

What is the purpose of Var_dump () in PHP?

To dump information about a variable, use the var_dump in PHP. This function provides structured data about the specified variable, including its type and value. Recursively, arrays and objects are examined, with values indented to demonstrate structure. This function works well with expressions as well.

What does Var_dump return?

@JMTyler var_export returns a parsable string—essentially PHP code—while var_dump provides a raw dump of the data. So, for example, if you call var_dump on an integer with the value of 1, it would print int(1) while var_export just prints out 1 .

What is the difference between Var_dump () and Print_r ()?

var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type. It will return a value that is in string format.

What value will Var_dump show?

The var-dump is display the datatype as well as value while echo only display the value. Explanation: In Php the var-dump function is used for displaying the datatype as well the value . The var-dump () does not return anythings it means there is no datatype of var-dump() function.