fwrite() example


fwrite() example

Here you will learn about the example of fwrite().

Here you will learn about the example of fwrite().

The function fwrite()

Writing on a PHP file is an easy task and you can write any text on a PHP file using fwrite(); command. For writing on a PHP, first open that file and use function fwrite() in conditional statement. If the program finds the conditions true, it will print the desired texts otherwise not. 

For Example: In the below example, we are trying to print 'Develop Name:Varun Rai'. Lets see this example in steps:

First of all, begin the script with PHP tag;

Open the file on which the text has to be printed

begin the conditional statement with filename ();

Input the fwrite() command within the middle brackets,

check the condition;

Close the program with PHP tag.

$File_name = fopen("mytextfile.txt", 'w+');

if(!fwrite($File_name, "Developer Name: Varun Rai"))
die("You can not write in the file.");

echo "Successfully writing to file";

In the above program, if the file exists and runs properly, it will print "Developer Name:Varun Rai" and "Successfully writing to file", if failed to do so, the output will be "you can not write in the file".