Skip to content
PHP Logo

How can one optimize the code snippet provided to check for the existence of a directory in PHP for better performance and efficiency?

The issue with the current code snippet is that it uses the `file_exists()` function to check for the existence of a directory, which is not the most efficient way to do so. To optimize the code for better performance and efficiency, we can use the `is_dir()` function instead, which specifically checks if a given path is a directory.

php
$directory = '/path/to/directory';

if (is_dir($directory)) {
    echo 'Directory exists';
} else {
    echo 'Directory does not exist';
}