D. The function name is misspelled; should be is_writable()
Solution
Step 1: Check function spelling
The correct function to check if a file is writable is is_writable(), not is_writeable().
Step 2: Verify other syntax
Semicolons are present, variable quotes are valid, and is_writable() does check permissions.
Final Answer:
The function name is misspelled; should be is_writable() -> Option D
Quick Check:
Correct spelling = is_writable() [OK]
Hint: Remember: writable has one 'e' after writ [OK]
Common Mistakes:
Misspelling is_writable() as is_writeable()
Thinking variable needs double quotes
Confusing permission check with existence
5. You want to list all files in a directory 'docs/' that exist, are readable, and have been modified in the last 24 hours. Which PHP code snippet correctly implements this?
B. $files = scandir('docs/');
foreach ($files as $file) {
if (is_dir($file)) continue;
if (is_writable($file)) {
echo $file . "\n";
}
}
C. $files = scandir('docs/');
foreach ($files as $file) {
if (file_exists($file) && filesize($file) > 0) {
echo $file . "\n";
}
}
D. $files = scandir('docs/');
foreach ($files as $file) {
if (is_file($file) && is_readable($file)) {
echo $file . "\n";
}
}
Solution
Step 1: Understand requirements
We need to list files in 'docs/' that exist, are readable, and modified within last 24 hours (86400 seconds).
Step 2: Analyze each option
$files = scandir('docs/');
foreach ($files as $file) {
$path = 'docs/' . $file;
if (file_exists($path) && is_readable($path) && (time() - filemtime($path) < 86400)) {
echo $file . "\n";
}
} correctly scans 'docs/', builds full path, checks file_exists(), is_readable(), and filemtime() against current time minus 86400 seconds. Other options miss one or more conditions or use wrong paths.
Final Answer:
Option A code snippet -> Option A
Quick Check:
Check existence + readability + recent mod = $files = scandir('docs/');
foreach ($files as $file) {
$path = 'docs/' . $file;
if (file_exists($path) && is_readable($path) && (time() - filemtime($path) < 86400)) {
echo $file . "\n";
}
} [OK]
Hint: Combine file_exists(), is_readable(), and filemtime() with full path [OK]
Common Mistakes:
Not using full path when checking files
Ignoring file modification time condition
Checking is_writable() instead of is_readable()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.