What is the purpose of using the fopen function in PHP and what are the potential modes that can be used?

The fopen function in PHP is used to open a file or URL for reading or writing. It allows you to interact with files on the server, such as reading data from a file or writing data to a file. The potential modes that can be used with fopen include "r" for reading, "w" for writing (and truncating the file to zero length), "a" for writing (appending to the end of the file), "r+" for reading and writing, and more.

$file = fopen("example.txt", "r");
if ($file) {
    // File opened successfully
    // Perform operations like reading data from the file
    fclose($file); // Close the file when finished
} else {
    echo "Error opening file.";
}