What potential pitfalls can arise when using the fwrite function in PHP, especially in relation to segmentation faults?

When using the fwrite function in PHP, potential pitfalls can arise if the function is not used correctly, leading to segmentation faults. To avoid this issue, always ensure that the file pointer is valid and that the data being written is properly formatted. Additionally, make sure to handle any errors that may occur during the writing process to prevent segmentation faults.

$filename = "example.txt";
$file = fopen($filename, "w");

if ($file) {
    $data = "Hello, World!";
    
    if (fwrite($file, $data) === false) {
        echo "Error writing to file";
    }
    
    fclose($file);
} else {
    echo "Error opening file";
}