Home Tutorial Php Examples fwrite() example

 
 

fwrite() example
Posted on: July 30, 2009 at 12:00 AM
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.

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

if($File_name)
{
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".

Related Tags for fwrite() example:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.