php: save PHP variables to a text file

Writing $text to a file:


1
2
3
4
5
$text = "Anything";
$var_str = var_export($text, true);
$var = "<?php\n\n\$text = $var_str;\n\n?>";
file_put_contents('filename.php', __FILE__ . ":". __LINE__ . "\t" . date(DATE_RFC2822) . "\n", FILE_APPEND | LOCK_EX);
file_put_contents('filename.php', $var, FILE_APPEND | LOCK_EX);

Retrieving it again:


1
2
include 'filename.php';
echo $text;
Scroll to top