Usage of fwrite function in php

下次还敢
Release: 2024-04-29 12:51:14
Original
1128 people have browsed it

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

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>
Copy after login

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>
Copy after login

The above code converts the string "Hello , world!" is written to a file named "myfile.txt".

Note:

  • fwrite will only start writing if the file is successfully opened.
  • fwrite will fail if the file is opened in read-only mode.
  • If the data written exceeds the given file size limit, fwrite will truncate the data.
  • After successful writing, you need to use the fclose() function to close the file.

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!