How can I access the for-loop variable outside the loop in PHP?

I have a for loop in PHP and want to access the $f variable outside the loop. Is there a certain way to do this? I searched for the answer but can't find the right one.

foreach($dirlist as $f) { } filemtime($f)

Update:

I just went the more simple way and ended up doing this:

date('M d, Y', filemtime(getcwd().'/index.php'))

-------------Problems Reply------------

Add your filemtime results to the new array and retrieve last modified date by file name.

<?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>

Consider the following pseudo-code:

$yourMatchedFile = null;
foreach($dirlist as $f) {
if($f is good enough!!) {
$yourMatchedFile = $f;
break;
}
}

if(isset($yourMatchedFile)) {
DoWhatever();
}

Category:php Views:2 Time:2018-08-09
Tags: php for loop

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.176 (s). 11 q(s)