PHP 7.1“require / include”结果不是最新的

I have a file containing:

<?php
    return '2000-01-01 00:00:00';
?>

and I have this code:

<?php

    $oldValue = require 'file.php';
    $now = new DateTime();
    $handle = fopen('file.php', "w");
    fputs($handle, "<?php
return '" . $now->format('Y-m-d H:i:s') . "';");
    fclose($handle);
    $newValue = require 'file.php';

    echo "Old value: $oldValue ";
    echo "New value: $newValue ";
?>

The output with PHP 5.3 is:

Old value: 2000-01-01 00:00:00 New value: 2018-03-28 10:33:12

The output with PHP 7.1 is:

Old value: 2000-01-01 00:00:00 New value: 2000-01-01 00:00:00

In the two cases, the string in the file changes.

Can some one help me to update the new value with PHP 7.1?

Note: it's not the real problem. It's just an abstraction of the problem to make things more simple and comprehensible. So please, no lessons of PHP best practices. I just like to get a good response to my question.

Thanks :)

As commented by iainn The issue is that the PHP server is caching the file once it's loaded and is not re-calling the file from disc on the second require, instead calling it from it's memory cache.

As you have stated that:

"the content of the file changes "

then the issue is the new contents are not passed to the script, instead using the memory of the older contents.

Therefore call clearstatcache() to force clear the cached file data. This should be placed after the new data is written to update the file, and before the file is called for a second time.

If this does not work then the file data may be cached elsewhere in its route.

<?php

    $oldValue = require 'file.php';
    $now = new DateTime();
    $handle = fopen('file.php', "w");
    fputs($handle, "<?php
return '" . $now->format('Y-m-d H:i:s') . "';");
    fclose($handle);
    clearstatcache();           // THIS line should help you 
    $newValue = require 'file.php';

    echo "Old value: $oldValue ";
    echo "New value: $newValue ";
?>

It could be an issue with your file being cached (OPcache) and php returning the same file in both require calls

Can you try modifying opcache settings

opcache.enable = 0

and then testing it ? Also there is

opcache_reset() 

could help you but if you running your code from CLI it may not work.