The SplFileObject::fwrite() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to write to the file.
Syntax:
php
Output:
php
Output:
int SplFileObject::fwrite( $str, $length )Parameters: This function accept two parameters as mention above and describe below:
- $str: It is used to specify the string which need to write to the file.
- $length: It is an optional parameter. If it is given then file writing will stop after a specified length.
<?php
// Create a file named "gfg.txt" if not exist
$gfg = new SplFileObject("gfg.txt", "w+");
// Write data in gfg.txt
$gfg->fwrite("GeeksforGeeks a CS Portal");
// Open file again in read mode
$gfg = new SplFileObject("gfg.txt");
// Print result after written
while (!$gfg->eof()) {
echo $gfg->fgetc();
}
?>
GeeksforGeeks a CS PortalProgram 2:
<?php
// Create an Array
$GFG = array(
"dummy.txt",
"gfg.txt",
"frame.txt"
);
// Creating Spl Object
foreach ($GFG as &$arr) {
$gfg = new SplFileObject($arr, "w+");
// Write data in file
$gfg->fwrite("GeeksforGeeks a CS Portal for Geeks");
// Open file again in read mode
$gfg = new SplFileObject("gfg.txt");
// Print result after written
while (!$gfg->eof()) {
echo $gfg->fgetc();
}
echo "</br>";
}
?>
GeeksforGeeks a CS Portal for Geeks GeeksforGeeks a CS Portal for Geeks GeeksforGeeks a CS Portal for GeeksReference: https://www.php.net/manual/en/splfileobject.fwrite.php