$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, "Content to write to file");
fclose($myfile);
// if not file is there then automatic create and write inside it.
$fptr = fopen('myfile.txt','w');
fwrite($fptr,"i am writing file in this
");
fwrite($fptr,'writing another line.');
fclose($fptr);
<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith
";
// Write the contents back to the file
file_put_contents($file, $current);
?>
$text = "Anything";
$var_str = var_export($text, true);
file_put_contents('filename.txt', $var_str);
<?php
$myfile = fopen("file_name.txt", "w") or die("Unable to open file!");
$txt = "Hello world
";
fwrite($myfile, $txt);
$txt = " Php.
";
fwrite($myfile, $txt);
fclose($myfile);
?>