title: fwrite writes NUL
body: <p>I'm trying to write to a file with PHP and this is the code I'm using (taken from <a href="https://stackoverflow.com/questions/18236599/visits-counter-without-database-with-php/18237397?noredirect=1#answer-18237034">this answer</a> to my previous question):</p>
  
  <pre><code>$fp = fopen("counter.txt", "r+");
  
  while(!flock($fp, LOCK_EX)) {  // acquire an exclusive lock
      // waiting to lock the file
  }
  
  $counter = intval(fread($fp, filesize("counter.txt")));
  $counter++;
  
  ftruncate($fp, 0);      // truncate file
  fwrite($fp, $counter);  // set your data
  fflush($fp);            // flush output before releasing the lock
  flock($fp, LOCK_UN);    // release the lock
  
  fclose($fp);
  </code></pre>
  
  <p>The read part works fine, if the file gets read, it's content is read well, i.e. if the file contains <code>2289</code>, then <code>2289</code> is read.</p>
  
  <p>The problem is that when it increments and rewrites the value to that file, <code>[NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL]1</code> gets written.</p>
  
  <p>What am I missing? Why do null characters get written?</p>