In PHP, what are the potential risks of using string interpolation with variable assignments in fwrite statements?

When using string interpolation with variable assignments in fwrite statements, the potential risk is that the variables may contain user input that could be malicious, leading to security vulnerabilities such as code injection or file manipulation. To mitigate this risk, it is important to sanitize and validate user input before using it in fwrite statements.

// Sanitize and validate user input before using it in fwrite statements
$userInput = $_POST['user_input']; // Example user input
$sanitizedInput = htmlspecialchars($userInput); // Sanitize user input

// Write sanitized input to a file using fwrite
$file = fopen("output.txt", "w");
fwrite($file, "Sanitized input: $sanitizedInput");
fclose($file);