Skip to content

is_dir()

What is the is_dir() Function?

The is_dir() function is a built-in PHP function that checks whether a given path is a directory. This function returns true if the path is a directory and false otherwise.

Here's the basic syntax of the is_dir() function:

The PHP syntax of is_dir()

php
is_dir(path);

Where path is the path to the directory you want to check.

How to Use the is_dir() Function?

Using the is_dir() function is straightforward. Here are the steps to follow:

  1. Specify the path to the directory you want to check.
  2. Call the is_dir() function, passing in the path as a parameter.
  3. Use the resulting boolean value to determine whether the path is a directory.

Here's an example code snippet that demonstrates how to use the is_dir() function:

How to Use the is_dir() Function?

php
<?php

$path = '/path/to/directory';
if (is_dir($path)) {
    echo 'The path is a directory';
} else {
    echo 'The path is not a directory';
}

In this example, we use the is_dir() function to check whether the path /path/to/directory is a directory. We then use a conditional statement to print out a message indicating whether the path is a directory or not.

Conclusion

The is_dir() function is a useful tool in PHP for checking whether a given path is a directory. By following the steps outlined in this guide, you can easily use the is_dir() function in your PHP projects to check whether paths are directories.

Practice

What is the purpose of the is_dir() function in PHP?

Dual-run preview — compare with live Symfony routes.