我在PHP中有一个for循环,并希望访问循环外的$f
变量。有特定的方法可以做到这一点吗?我寻找答案,但找不到正确的。
foreach($dirlist as $f) {
}
filemtime($f)
更新:
我只是用了更简单的方法,最后这样做:
date('M d, Y', filemtime(getcwd().'/index.php'))
将您的filemtime
结果添加到新的数组中,并根据文件名检索最后修改的日期。
<?php
// Array of modified dates, file names as keys
$modifiedDates = array();
foreach($dirlist as $f) {
$modifiedDates[basename($f)] = filemtime($f);
}
// File name to look modified time again (just file name, no path)
// basename function gets file name from path
$filename = basename( $pathToFile );
$lastModified = $modifiedDates[$filename];
?>
<p class="post-info">This article was last reviewed on <?php echo '<time itemprop="dateModified" datetime="'.date('Y-m-d', $lastModified).'">'. date('M d, Y', $lastModified).'</time>'; ?></p>
考虑以下伪代码:
$yourMatchedFile = null;
foreach($dirlist as $f) {
if($f is good enough!!) {
$yourMatchedFile = $f;
break;
}
}
if(isset($yourMatchedFile)) {
DoWhatever();
}