![]() |
VOOZH | about |
The fputs() function in PHP is an inbuilt function which is used to write to an open file. The fputs() 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 that has to be written are sent as parameters to the fputs() function and it returns the number of bytes written on success, or FALSE on failure. The fputs() function is an alias of the fwrite() function.
Syntax:
fputs(file, string, length)Parameters:
The fputs() function in PHP accepts three parameters.
Return Value:
Exceptions
Examples:
Input : $myfile = fopen("gfg.txt", "w");
echo fputs($myfile, "Geeksforgeeks is a portal of geeks!");
fclose($myfile);
Output : 35
Input : $myfile = fopen("gfg.txt", "w");
echo fputs($myfile, "Geeksforgeeks is a portal of geeks!", 13);
fclose($myfile);
fopen("gfg.txt", "r");
echo fread($myfile, filesize("gfg.txt"));
fclose($myfile);
Output : Geeksforgeeks
Below programs illustrate the fputs() function:
In this example, we use the fputs() function to write an entire string to a file. The program opens the file, writes the string "Geeksforgeeks is a portal of geeks!" to it, and then displays the number of bytes written to the file.
Output:
35This example shows how to use the optional length parameter in the fputs() function to write only a portion of a string to a file. The program writes the first 13 bytes of the string "Geeksforgeeks is a portal of geeks!" and then reads and displays the contents of the file.
Output:
Geeksforgeeks