var_dump to fwrite

Hi all can any one tell me why this peace of code is not working im trying to write var_dump into a file.


$data = var_dump($GLOBALS);

$fp = fopen("textfile.txt", "w");

fwrite($fp, $data);

fclose($fp);

Chears

Because var_dump prints to std output. It doesn’t return the data.

You can capture the output with an output buffer:


ob_start();
var_dump($GLOBALS);
$data = ob_get_clean();
$fp = fopen("textfile.txt", "w");
fwrite($fp, $data);
fclose($fp);

Or you can use print_r with second parameter set to true.

Ah! ok thank you for that kyberfabrikken :slight_smile: