How can a recursive function be used to process directory paths in PHP efficiently?

When processing directory paths in PHP recursively, it is important to efficiently navigate through nested directories to avoid performance issues. One way to achieve this is by using a recursive function that can traverse directories and their subdirectories effectively. This function should handle each directory it encounters by recursively calling itself to process its contents.

function processDirectory($path) {
    $files = scandir($path);
    
    foreach($files as $file) {
        if ($file != '.' && $file != '..') {
            $fullPath = $path . '/' . $file;
            
            if (is_dir($fullPath)) {
                processDirectory($fullPath); // Recursive call to handle subdirectories
            } else {
                // Process the file here
                echo $fullPath . "\n";
            }
        }
    }
}

// Usage example
processDirectory('/path/to/directory');