【发布时间】:2013-12-24 20:19:39
【问题描述】:
我有一串这样的代码:
<?php
echo "Hello World";
?>
现在,我想把它写到一个 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);
【问题讨论】:
-
我不明白,您期望的输出是什么?会发生什么?
-
你想完成什么?
-
也许问题出在编辑器上而不是你的代码上?就像许多人说的那样,您需要
\n才能获得新线路。你用的是什么编辑器? -
您是否正在尝试将 PHP 文件复制到另一个文件中? (如果是这样,请使用copy)。 [或者是你想通过网络向用户输出内容吗?]
-
哈!这很容易!!!!!!