Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing PHP Code to a File

Tags:

php

quotes

I am wondering if it is possible to write php code to a file. For example:

fwrite($handle, "<?php $var = $var2 ?>");

I would like it produce the exact string in the file and not the eval'ed code. Is this possible?

I am currently getting the following output (where $var = 1 and $var2 = 2):

<?php 1 = 2 ?>

Whereas I want the actual string:

<?php $var = $var2 ?>

Thanks for the help

like image 242
Speedy Avatar asked Nov 28 '22 00:11

Speedy


2 Answers

You can use single quotes instead of double quotes, which do not expand inline variable names:

fwrite($handle, '<?php $var = $var2 ?>');
like image 146
Joey Avatar answered Dec 10 '22 09:12

Joey


Just escape the $ symbol

<?php
fwrite($handle, "<?php \$var = \$var2 ?>");
?>
like image 40
perrohunter Avatar answered Dec 10 '22 08:12

perrohunter