PHP SplFileObject fwrite () function

PHP

Syntax:
int SplFileObject::fwrite ($str, $length)
Parameters:This function takes two parameters as mentioned above and described below:
  • $str:is used to specify the string to be written to the file.
  • $length:This is an optional parameter. If specified, writing to the file will stop after the specified length.
Returned Values:This function returns the number of bytes written to the file, and 0 on error. The following programs illustrate the SplFileObject::fwrite() function in PHP:Program 1:  
// Create a file named & quot; gfg.txt & quot; if it does not exist $gfg = new SplFileObject ( "gfg.txt" , "w +" );  
// Write data to gfg.txt $gfg -> fwrite ( "GeeksforGeeks a CS Portal" );  
// Reopen the file in Reading View $gfg = new SplFileObject ( " gfg.txt " );  
// Print the result after writing while (! $gfg -> eof()) { echo $gfg -> fgetc (); }
?>
Output:
GeeksforGeeks a CS Portal
Program 2:  
// Create array $GFG = array ( "dummy.txt" , "gfg.txt" , "frame.txt" );  
// Create a Spl object foreach ( $GFG as & amp; $arr ) { $gfg = new SplFileObject ( $arr , "w +" ); // Write data to file $gfg -> fwrite ( "GeeksforGeeks a CS Portal for Geeks" ); // Reopen the file in Reading View $gfg = new SplFileObject ( "gfg.txt " ); // Print the result after writing while (! $gfg -> eof()) { echo $gfg -> fgetc (); } echo " < / br > " ; }
?>
Output:
GeeksforGeeks a CS Portal for Geeks GeeksforGeeks a CS Portal for Geeks GeeksforGeeks a CS Portal for Geeks
Link: http://php.net/manual/en/splfileobject.fwrite.php