In what scenarios should recursive functions or loops be used for creating directories in PHP scripts?

When creating directories in PHP scripts, recursive functions or loops should be used when dealing with nested directory structures or when creating directories within directories. This is necessary to ensure that all directories are created in the correct order and that any subdirectories are also created if needed.

function createDirectory($path) {
    if (!is_dir($path)) {
        mkdir($path, 0777, true);
    }
}

// Example usage:
createDirectory('parent/child/grandchild');