0

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"; \n ';
    $datas .= '$text_2 = "Banana"; \n ';

    $datas = nl2br("&lt;? \n".$datas."\n ?&gt;");

    fwrite($file, $datas);
    fclose($file);
?>

My problem is the generated php looks like this:

&lt;? <br />
$text_1 = "Text"; \n$text_2 = "Banana"; \n<br />
 ?&gt;
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • 1
    Can you please check below link : [http://stackoverflow.com/questions/3066421/writing-a-new-line-to-file-in-php](http://stackoverflow.com/questions/3066421/writing-a-new-line-to-file-in-php) – Bhavin Solanki Jul 11 '16 at 16:57

3 Answers3

0

You need to wrap \n 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.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
0

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);
?>
Luigi Pressello
  • 937
  • 4
  • 9
  • I would avoid using Heredoc if at all possible. It's a terrible way to code. – Machavity Jul 11 '16 at 17:09
  • Why are you saying that? Heredoc will be a lot more clearer if you need to write the code for a file and, of course, the name heredoc says it all: https://en.m.wikipedia.org/wiki/Here_document So don't hate me if I say that definitely heredoc is the more correct way to go. – Luigi Pressello Jul 11 '16 at 17:17
  • The single largest drawback is you cannot indent heredoc. Even within a function. I don't hate you (I didn't even downvote it), I just don't recommend it – Machavity Jul 11 '16 at 17:25
  • But as the linked Wikipedia page says: it's a file in a file, do there's no good reason to keep following indentation, there will be to many issues. How you will decide when to indent the file output and when not in relation to your generator code? If you choose to indent always you'll be soon arrive to pieces of generated code that are visually indented many more times than the actual generated code due to the indentation of the generator and that will make the code more hard to read than a simply heredoc. BTW I think that anyone chooses the way to code that finds more confortable. – Luigi Pressello Jul 11 '16 at 17:30
0

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 \n" .
    '$text_1 = "Text"; ' . "\n" .
    '$text_2 = "Banana";';
fwrite($file, $datas);
Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100