溫馨提示×

PHP put如何向文件寫入內(nèi)容

PHP
小樊
83
2024-07-31 13:26:08
欄目: 編程語言

你可以使用PHP中的file_put_contents()函數(shù)來向文件寫入內(nèi)容。示例如下:

$file = 'example.txt';
$content = 'Hello, world!';

file_put_contents($file, $content);

上面的代碼將字符串"Hello, world!"寫入到一個名為example.txt的文件中。如果文件不存在,則會創(chuàng)建一個新文件。如果文件已存在,則會覆蓋原有內(nèi)容。如果你想要在文件末尾追加內(nèi)容而不是覆蓋,可以傳入FILE_APPEND作為第三個參數(shù),示例如下:

$file = 'example.txt';
$content = 'Hello, world!';

file_put_contents($file, $content, FILE_APPEND);

這樣就會在文件末尾追加內(nèi)容而不是覆蓋原有內(nèi)容。

0