How can one effectively handle errors or warnings related to opendir() in PHP scripts?

When handling errors or warnings related to opendir() in PHP scripts, it is important to check if the directory exists before attempting to open it. This can prevent errors and warnings from occurring. Additionally, using error handling functions like try-catch blocks or using the error_reporting() function can help in effectively managing any potential issues.

$dir = 'path/to/directory';

if (is_dir($dir)) {
    $handle = opendir($dir);
    // Rest of the code to handle the directory contents
} else {
    echo "Directory does not exist.";
}