DEV Community

Baransel
Baransel

Posted on

Writing to file with PHP

We will use the w mode and the fwrite() function to write files. The content of the file that we access only when using the w mode is completely deleted and replaced with the value we will write.

If you want to write without losing pre-existing content then you will need to use the a mode.

This function works with two parameters. In the first parameter we write the variable that we access the file with fopen(), and in the second parameter we write the content to be written.

<?php
$file = fopen('hello.txt', 'w');
fwrite($file, 'Hello World');
fclose($file);
Enter fullscreen mode Exit fullscreen mode

When we run the example above, we have written Hello World in the content of our file named hello.txt.

Follow my blog for more baransel.dev.

Top comments (0)