How can PHP be used to open a local file using a variable as part of the file name?

To open a local file using a variable as part of the file name in PHP, you can concatenate the variable with the file path within the fopen() function. This allows you to dynamically specify the file name based on the value of the variable.

$filename = "example.txt";
$file = fopen($filename, "r");
if ($file) {
    // File opened successfully, do something with the file
    fclose($file);
} else {
    echo "Error opening file.";
}