Home / PHP Tips Generated By ChatGPT / Writing a String to a File in PHP Use file_put_contents() to write a string to a file, creating the file if it doesn’t exist. Source Code $text = "Hello, world!"; file_put_contents("hello.txt", $text);