in reply to Writing to file

You might start testing your hypothesis (see moritz reply, above) by double-checking your line 13. One obvious test (and this deal with the issue raised by SteveS832001) would be to try a different directory to write to.

However, it just so happens that I'm not at all sure that's the problem.

Works for me (update:) if allowed to run for approx 15+ iterations (precise count not determined. The shock was too great... and this oddity was discovered only when I found that at significantly higher counts, the content of OUTPUT.TXT is NOT in the order which I had expected from your code, nor from the display to the terminal.)

update2

Hunh??? /methinks there may be some buffering issues, as my output file is NOT ALWAYS misordered, but if not, tends to have lost some of the output seen on screen.)

FTR, the adjustments I made to OP's script were limited to changing the quotes to singles (and later, redirecting output to another drive) and testing on localhost (127.0.0.1) where I have (intranet) Apache running.