In what scenarios would using file_put_contents be a suitable alternative to fwrite for writing data to a text file in PHP?

file_put_contents is a suitable alternative to fwrite when you want to write data to a text file in PHP in a more concise and straightforward manner. It simplifies the process by handling the file opening, writing, and closing operations internally. This function is particularly useful when you want to write data to a file in one go without the need for complex file handling logic.

$data = "Hello, World!";
$file = "example.txt";

file_put_contents($file, $data);