Home  >  Article  >  Backend Development  >  How to write files in php

How to write files in php

藏色散人
藏色散人Original
2019-09-18 09:46:199516browse

How to write files in php

php method of writing files

PHP writing files-fwrite()

fwrite() function is used to write files.

The first parameter of fwrite() contains the file name of the file to be written, and the second parameter is the string to be written.

The following example writes the name to a new file named "newfile.txt":

Example

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Bill Gates\n";
fwrite($myfile, $txt);
$txt = "Steve Jobs\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

Please note that we write the name to the file "newfile.txt" Wrote it twice. Each time we write to the file, the string $txt we send contains "Bill Gates" the first time and "Steve Jobs" the second time. After writing is complete, we use the fclose() function to close the file.

If we open the "newfile.txt" file, it should look like this:

Bill Gates
Steve Jobs

For more PHP knowledge, please visit the PHP Chinese website PHP Tutorial!

The above is the detailed content of How to write files in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to print array in phpNext article:How to print array in php