使用return语句时清除PHP文本文件

To start out with, I basically have a function that opens up a text file, grabs the first line, removes it from the file, and saves the file with the remainder. Everything is working fine except when I come to use the return statement to return the line retrieved. Using the return statement, for some reason it clears the file; removing the return statement, the function does what it's supposed to. I'm debating whether this is a PHP bug or if there's something hidden here that I'm not seeing. Parts of the code is listed below, any help will be immensely appreciated.

Thanks!

    $nlines = array();

    $lines  = file(CACHE_PATH."/test.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    $link  = $lines[0];

    $lcount = count($lines)-1;
    for($i=1; $i < $lcount; $i++)
    {
        $nlines[] = $lines[$i];
    }

    $file  = fopen(CACHE_PATH."/test.txt", 'w');

    for($i=0; $i <= $lcount; $i++)
    {
        fwrite($file, $nlines[$i]);
    }

    fclose($file);

    return $link;

The code seems aright to me. However here is a simplified version. Its untested though:

function something(){

    $lines  = file(CACHE_PATH."/test.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $link  = $lines[0];
    unset($lines[0]);

    $file  = fopen(CACHE_PATH."/test.txt", 'w');
    fwrite($file, implode("
" , $lines) );
    fclose($file);

    return $link;

}