This is an easy way to have a webpage with a link that, when clicked, saves a variable to an output (like a text file) without leaving the page.

{codecitation}
session_start();
//test.php
if(isset($_SESSION['code'])){
  unset($_SESSION['code']);
}
$_SESSION['code'] = "<?php echo 'hello'; ?>";
{/codecitation}{codecitation class="brush:html"}
<a href="/test2.php">click here to save</a>{/codecitation}

{codecitation}
session_start();
//test2.php
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="test.php"');
header("Content-Transfer-Encoding: binary");
print $_SESSION['code'];
unset($_SESSION['code']); //if you want to allow them to download the variable code more than once comment this line
{/codecitation}