I'm tryning to create a new php file from php.
My final php file must look like this:
<?
$text_1 = "Text";
$text_2 = "Banana";
?>
Actually my php to generate it is the following:
$file = fopen("../lang/_____fr-CA.php", "w") or die("Unable to open file!");
<?
$datas .= '$text_1 = "Text";
';
$datas .= '$text_2 = "Banana";
';
$datas = nl2br("<?
".$datas."
?>");
fwrite($file, $datas);
fclose($file);
?>
My problem is the generated php looks like this:
<? <br />
$text_1 = "Text";
$text_2 = "Banana";
<br />
?>
You need to write the data you want. In your case, you're writing HTML. You should write the exact PHP code, as it should appear. You don't need nl2br
. You also need to understand that newlines must be in double quotes. Finally, the ?>
is optional, and I would recommend you omit it for a pure PHP file.
$datas = "<?php
" .
'$text_1 = "Text"; ' . "
" .
'$text_2 = "Banana";';
fwrite($file, $datas);
You need to wrap in double quotes, it won't be evaluated as a line break in single quotes.
For compatibility purposes, you should never use the short open tag <?
, you should only use <?php
.
You can use a more "elegant" way like the heredoc to write your file:
<?
$datas <<<eof
<?php
$text_1 = "Text";
$text_2 = "Banana";
?>
eof;
fwrite($file, $datas);
fclose($file);
?>