Welcome to another exciting tutorial on CodeYourCraft! Today, we're diving into the PHP function rmdir(), which is used to remove directories. Let's get started! π
rmdir() is a PHP function that removes a directory if it is empty. If the directory is not empty, it will return FALSE.
Here's a simple example of how to use rmdir():
<?php
$dir = 'example_directory';
if (rmdir($dir)) {
echo $dir . ' has been deleted.';
} else {
echo 'Unable to delete ' . $dir;
}
?>In this example, we're trying to delete a directory named example_directory. If the directory is successfully deleted, it will print "example_directory has been deleted." If not, it will print "Unable to delete example_directory."
If the directory you're trying to delete contains files or subdirectories, rmdir() will return FALSE. To overcome this, you can use the recursive argument in rmdir():
<?php
$dir = 'example_directory';
if (is_dir($dir) && rmdir($dir, true)) {
echo $dir . ' has been deleted.';
} else {
echo 'Unable to delete ' . $dir;
}
?>In this example, we're using the is_dir() function to check if $dir is a directory. If it is, we're passing the true argument to rmdir() to indicate that we want to delete the directory and its contents recursively.
rmdir() will not delete the directory immediately. It will only mark the directory for deletion. The actual deletion happens when the system's garbage collector runs.Answer: B
Explanation: If the directory is empty, rmdir() will return TRUE, and the code will print "example_directory has been deleted." If the directory is not empty, it will return FALSE.