PHP fwrite() Function

PHP fwrite Function


Definition : 

The PHP fwrite() function writes the specified string to the specified file stream. If the length argument is provided, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.

Syntax

  fwrite(filestringlength)

Parameter 
Parameter Description
file Required. Specifies the open file to write to
string Required. Specifies the string to write to the open file
length Optional. Specifies the maximum number of bytes to write

Example: Writing to a file using write mode

Lets assume that we have a file called demo.txt. This file contains following content:

This is a test file.
It contains dummy content.

In the example below, the file is opened using 'w' mode. This places the file pointer at the beginning of the file and deletes its previous content. Hence, fwrite() writes from the start of the file.

Output : A fresh content is added.