Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write output of print_r in a txt file. PHP [duplicate]

Tags:

php

Possible Duplicate:
Print array to a file

How can I write the ouput (for example when I use an array), in a file?, I was trying with this:

    ...
print_r($this->show_status_redis_server());
$status_redis = ob_get_contents();
fwrite($file, $status_redis);
    ...
like image 439
itaka Avatar asked Nov 13 '12 13:11

itaka


2 Answers

print_r() has a second parameters that if passed as TRUE returns the output as a string.

$output = print_r($data, true);
file_put_contents('file.txt', $output);

You could even cosinder using var_export function, as it provides better information about data types. From print_r you can't tell if the variable is NULL of FALSE, but var_export lets use see exactly the data type of a variable.

like image 124
Mārtiņš Briedis Avatar answered Oct 22 '22 21:10

Mārtiņš Briedis


print_r($expression [, bool $return = false ]) has optional parameter that identifies you want to return string or echo one.

$str = print_r($desiredVariable, true);
fwrite($handle, $str);

Also I'd use file_put_contents:

$content = print_r($yourVar, true);
file_put_contents('file.log', $content);
like image 4
Leri Avatar answered Oct 22 '22 22:10

Leri