The fwrite() function in PHP is an inbuilt function which is used to write to an open file. The fwrite() function stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first. The file, string and the length which has to be written are sent as parameters to the fwrite() function and it returns the number of bytes written on success, or FALSE on failure.
fwrite(file, string, length)
Parameters: The fwrite() function in PHP accepts three parameters.
- file : It is a mandatory parameter which specifies the file.
- string : It is a mandatory parameter which specifies the string to be written.
- length : It is an optional parameter which specifies the maximum number of bytes to be written.
Return Value: It returns the number of bytes written on success, or False on failure.
- Both binary data, like images and character data, can be written with this function since fwrite() is binary-safe.
- If writing operation is performed twice to the file pointer, then the data will be appended to the end of the file content.
Input : $myfile = fopen("gfg.txt", "w"); echo fwrite($myfile, "Geeksforgeeks is a portal for geeks!"); fclose($myfile); Output : 36 Input : $myfile = fopen("gfg.txt", "w"); echo fwrite($myfile, "PhP is Simple to Learn!"); echo fwrite($myfile, "Geeksforgeeks is a portal for geeks!"); fclose($myfile); Output : 23 59
Below programs illustrate the fwrite() function:
Reference : http://php.net/manual/en/function.fwrite.php
- PHP | SplFileObject fwrite() Function
- How to get the function name inside a function in PHP ?
- p5.js | arc() Function
- CSS | hsl() Function
- p5.js | tan() function
- p5.js | sin() function
- PHP | abs() Function
- PHP | Ds\Set first() Function
- PHP | cos( ) Function
- PHP | tan( ) Function
- PHP | sin( ) Function
- PHP | Ds\Set contains() Function
- D3.js | d3.lab() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.