-1

I tried putting this "Last Modified" piece into a function so I can call it via one function name instead of having to include all of this code in my HTML file. I want the PHP code in the Controller file.

Code via my PHP Controller

function lastModified()
{
    $filename = 'index.php';
    if (file_exists($filename)) 
    {
        echo "$filename was last modified: " . date ("F d Y     H:i:s.",filemtime($filename));
    }
}

Code via my HTML File

<div id="footer">
    <p><a href="/php/day%207%20code/before/addjoke/">IDJB Home</a>  -  <a href="?addjoke">Add Joke to IDJB</a>  -  <a href="#">Sitemap</a></p>
    ***LINE 36*** <p>&copy; <?php echo date("Y") ?> Internet Joke Database</p>  <?php echo $lastModified() ?>
</div>

I'm getting the following errors: I marked line 36 in form.html.php.

1.) Notice: Undefined variable: lastModified in C:\wamp\www\php\day 7 code\before\addjoke\form.html.php on line 36

2. ) Fatal error: Function name must be a string in C:\wamp\www\php\day 7 code\before\addjoke\form.html.php on line 36

tommydevs
  • 147
  • 2
  • 11
  • Works fine for me.. Result: `index.php was last modified: December 12 2014 22:02:58.` – Wade May 26 '15 at 05:13
  • Since filemtime() has been around forever (PHP 4+ about 2002).. The only issue you could have is either A) file doesn't exist B) file permission problems C) you didn't call the function: `lastModified()` -- Please show us your error, or we can't help you any further.. – Wade May 26 '15 at 05:16
  • please put more details like what is the outputted error; the file in which it is used; how you used the function; and how you included the file that contains the function... – catzilla May 26 '15 at 05:46
  • I updated my question. Sorry about that guys. – tommydevs May 26 '15 at 06:35
  • 1
    Well, you're trying to call a *variable function* there by having a `$` in front of your function call. That's probably not what you intend. – deceze May 26 '15 at 06:44

1 Answers1

1

Simply you have to delete $ before your function is calling: $lastm() should be lastmodified(), the style you are calling is unkown as closure, defined like this:

$func = function() { return "Hi"; } 
echo $func();

function func2() { return "Bye"; }
echo func2();
Alex
  • 715
  • 5
  • 14