What are the advantages of using file_put_contents() over fopen() and fwrite() for writing data to a file in PHP?

Using file_put_contents() in PHP is advantageous over fopen() and fwrite() for writing data to a file because it simplifies the process by combining the opening, writing, and closing of a file into a single function call. This reduces the amount of code needed and makes the operation more concise and readable. Additionally, file_put_contents() handles error checking and file locking automatically, making it a more convenient option for basic file writing tasks.

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

// Using file_put_contents()
file_put_contents($file, $data);

// Using fopen() and fwrite()
$handle = fopen($file, "w");
fwrite($handle, $data);
fclose($handle);