如何写文件原样?

San*_*hod -6 php newline

我有一串这样的代码:

<?php
echo "Hello World";
?>
Run Code Online (Sandbox Code Playgroud)

现在,我想把它写成一个php文件就像它一样.我的意思是我想用新的行字符编写这段代码.所以代码以上面的方式存储.那么,我该怎么做呢?

我曾尝试使用文件附加fwrite,但是他们在一个连续的行中编写代码,我想要的是将它分成3行.

  • 在第一行 - <?php
  • 在第二行 - echo "Hello world";
  • 在第三行 - ?>

但我想通过只使用一行代码来完成它,就像下面的那样.

$file = 'people.php';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
Run Code Online (Sandbox Code Playgroud)

ehi*_*ime 5

使用heredoc应该可以正常工作:

file_put_contents('people.php', <<<HEREDOC
<?php
echo "Hello World";
?>
HEREDOC
);
Run Code Online (Sandbox Code Playgroud)