Why does the fwrite() function in PHP expect a resource parameter and how can you provide it correctly?

The fwrite() function in PHP expects a resource parameter because it is used to write to a file handle, which is represented as a resource in PHP. To provide the resource parameter correctly, you need to first open the file using fopen() and pass the file handle resource returned by fopen() to the fwrite() function.

$file = fopen("example.txt", "w");
fwrite($file, "Hello, World!");
fclose($file);