PHP | SplFileObject fwrite() Function
Last Updated :
20 Dec, 2018
The SplFileObject::fwrite() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to write to the file.
Syntax:
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.
Return values: This function returns the number of bytes written to the file and 0 on error.
Below Programs illustrate the SplFileObject::fwrite() function in PHP:
Program 1:
<?php
$gfg = new SplFileObject( "gfg.txt" , "w+" );
$gfg ->fwrite( "GeeksforGeeks a CS Portal" );
$gfg = new SplFileObject( "gfg.txt" );
while (! $gfg ->eof()) {
echo $gfg -> fgetc ();
}
?>
|
Output:
GeeksforGeeks a CS Portal
Program 2:
<?php
$GFG = array (
"dummy.txt" ,
"gfg.txt" ,
"frame.txt"
);
foreach ( $GFG as & $arr ) {
$gfg = new SplFileObject( $arr , "w+" );
$gfg ->fwrite( "GeeksforGeeks a CS Portal for Geeks" );
$gfg = new SplFileObject( "gfg.txt" );
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
Reference: http://php.net/manual/en/splfileobject.fwrite.php
Share your thoughts in the comments
Please Login to comment...