For part of my website I need to be able to write php code to a file with php. For example:
$filename = \"RtestR.php\";
$ourFileName =$filename;
$ourFileHand         
        It seems relevant to mention php's HEREDOC in this context, e.g.:
<?php
$filename      = 'RtestR.php';
$ourFileName   = $filename;
$ourFileHandle = fopen($ourFileName, 'w');
$write =  <<<"FILE_CONTENTS"
<p>I like the color <?={$cleanSessionVars[ 'color' ]};?>.</p>
FILE_CONTENTS;
fwrite($ourFileHandle, $write);
fclose($ourFileHandle);
                                                                        Finally figured this out!
I needed to escape my $ symbols!
Like this:
$written =  "
<html>
<body>
<?php
echo \"I like the color \".\$_SESSION['color'].\"!!!!\";
</body>
</html>
";
Can't believe i didn't think of that ;)
Thank you all!
Hello pattyd: Nice to see ya :) + Don't upvote/accept this answer:
I would suggest simplified in this way:
$written =  "
    <html>
        <body>
           I like the color \" $_SESSION['color'] \" !!!!
        </body>
    </html>
";
                                                                        $fp = fopen("test.php", "w");
$string = '<html>
    <body>
    <?php
    echo "I like the color ".$_SESSION[\'color\']."!!!!";
    ?>
    </body>
    </html>';
fwrite($fp, $string);
fclose($fp);
                                                                        You can do like this :-
    <?php
    $filename = "RtestR.php";
    $ourFileName =$filename;
    $ourFileHandle = fopen($ourFileName, 'w');
    $written =  "<html>
                    <body>          
                        I like the color ".$_SESSION['color']."!!!! 
                    </body>
                </html> ";
    fwrite($ourFileHandle,$written);
    fclose($ourFileHandle);
?>
                                                                        You're missing your closing PHP tag ?>.
Consider for a moment that what you are doing might not be the best approach anyway. The only use case I can think of for writing out PHP files with PHP would be for some compiled template code or weird caching.