Home  >  Article  >  Backend Development  >  How to write to a file using PHP's fwrite() function

How to write to a file using PHP's fwrite() function

不言
不言Original
2019-02-23 14:41:164549browse

The fwrite() function in How to write to a file using PHPs fwrite() function is used to write data to files. In this article, we will take a look at how PHP's fwrite() function writes files.

How to write to a file using PHPs fwrite() function

The basic syntax of the fwrite() function is as follows:

 fwrite(file, string, length)

1. file specifies the file name to be written
2. String specifies the string to be written to the file
3. Lenght specifies the maximum number of bytes to be written to the file (optional)

This function returns the total number of bytes written to the file, or Returns false on failure.

The following is an example of writing data to a file using How to write to a file using PHPs fwrite() function.

In the example below, the first line will open myfile.txt in edit mode in the current directory. The second line writes the string to the file, where you can also specify the length and generate the number of bytes written. On failure, false will be returned. The third line will close the open file.

The code is as follows:

<?How to write to a file using PHPs fwrite() function
  $fn = fopen("myfile.txt","w");
  $result = fwrite($fn,"Welcome to PHP!");
  fclose($fn);
?>

This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to write to a file using PHP's fwrite() function. 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