Writing php to file using php

Howdy! I’m testing some code to create and write to a file which works well as far as functionality. However, when I try to write php code with variables in it, the variables get stripped out. If someone could tell me how to prevent that from happening, I’d appreciated it. Here is the code I’m using.

<?php error_reporting(E_ALL); ?>
<form action="" method="post">
<label for="add_header_text_opt">Add Header Option</label>
<input type="checkbox" name="add_header_text_opt" value="Yes" />
<p><input type="submit" name="submit" value="Create Options" class="button" /></p>
</form>
<?php

if(isset($_POST['submit']) && isset($_POST['add_header_text_opt']) && $_POST['add_header_text_opt'] = "Yes") {
   $test_file_create = "testFile.php";
      $mk_test_file = fopen($test_file_create, 'a+'); 
if($mk_test_file == false) {
   die("File does't exist or cannot be accessed. You may not have permission to access this file. Check the directory security settings.");
} else {
   $mk_settings_form = "<?php 
      $variable1 = 'Some text.'
      $variable2 = 'Some other text.'	
	  echo $variable1 . $variable2; ?>";
fwrite($mk_test_file, $mk_settings_form);
//test and form created
      echo file_get_contents($test_file_create);
	}
	fclose($mk_test_file);
	}
?>

Thanks much

Thank you, that’s helpful for small blocks of code like the one I used as an example. However, I have a large block of code with single quotes enclosing my parameters in many functions it will not work. Is there another way?

Hi, please try this way. The way you are doing it with two " quotes parses the variable, so use single quotes like this:

$mk_settings_form = '
<?php 
     $variable1 = "Some text.";
     $variable2 = "Some other text.";

     echo $variable1 . $variable2; ?>
';