What is the best practice for filling a PHP file with HTML content using fwrite?

When filling a PHP file with HTML content using fwrite, it is best practice to open the file in write mode, write the HTML content to the file using fwrite, and then close the file to ensure that the changes are saved.

<?php
// Open the file in write mode
$file = fopen("example.php", "w");

// HTML content to be written to the file
$html_content = "<html><head><title>Example</title></head><body><h1>Hello, World!</h1></body></html>";

// Write the HTML content to the file
fwrite($file, $html_content);

// Close the file
fclose($file);
?>