What potential issues can arise when creating and writing to files in PHP, as seen in the provided code snippets?

One potential issue that can arise when creating and writing to files in PHP is not properly handling errors that may occur during the file operations. It's important to check for errors and handle them gracefully to prevent unexpected behavior or data loss. One way to solve this is by using error handling functions like `file_put_contents` or `fopen` with appropriate error checking.

// Example of error handling when writing to a file using file_put_contents
$file = 'example.txt';
$data = 'Hello, World!';

if (file_put_contents($file, $data) === false) {
    echo "Error writing to file.";
} else {
    echo "Data written successfully.";
}