这是我的php代码:
function readDirectory($path) {
$handle = opendir($path);
while( $item = readdir($handle) !== false) {
if($item != "." && $item != "."){
if (is_file($path . "/" . $item)) {
$arr ['file'] [] = $item;
}
if (is_dir($path . "/" . $item)) {
$arr ['dir'] [] = $item;
}
}
}
closedir($handle);
return $arr; //line 24
}
$path = "file";
print_r(readDirectory($path));
但不断发现这些错误:
Notice: Undefined variable: arr in /Users/tyrant/workspace/apache/fileManager/dir.func.php on line 24
如何解决此问题?
答案 0 :(得分:0)
在你的函数中构建一个空的arr
function readDirectory($path) {
$handle = opendir($path);
$arr = array();
while( $item = readdir($handle) !== false) {
if($item != "." && $item != "."){
if (is_file($path . "/" . $item)) {
$arr ['file'] [] = $item;
}
if (is_dir($path . "/" . $item)) {
$arr ['dir'] [] = $item;
}
}
}
closedir($handle);
return $arr; //line 24
}
$path = "file";
print_r(readDirectory($path));
因此,如果您的逻辑触发
,它将返回一个数组和一个填充数组答案 1 :(得分:0)
你需要创建$ arr实例,就像那样:$ arr = array();