fwrite is used to write data to an opened file. The syntax is fwrite($handle, $string, $length = NULL), where $handle is the file pointer and $string is the data to be written. $length is the number of bytes to write (optional). fwrite returns the number of bytes written on success, or FALSE on failure.
Usage of fwrite function in PHP
What is fwrite?
fwrite is a built-in function in PHP that is used to write data to an open file.
Syntax:
<code class="php">int fwrite(resource $handle, string $string, int $length = NULL)</code>
Parameters:
$handle
: Opened File pointer, returned by the fopen() function. $string
: The data (string) to be written to the file. $length
: Number of bytes to write (optional). Default is strlen($string). Return value:
fwrite returns the number of bytes written when the data is successfully written, and returns FALSE when the writing fails.
Usage:
After opening the file, you can use the fwrite function to write data to the file:
<code class="php">$handle = fopen("myfile.txt", "w"); fwrite($handle, "Hello, world!"); fclose($handle);</code>
The above code converts the string "Hello , world!" is written to a file named "myfile.txt".
Note:
The above is the detailed content of Usage of fwrite function in php. For more information, please follow other related articles on the PHP Chinese website!