title: Write to file in PHP with a variable as the filename
body: <p>I have a script that will write a given string to a text file...</p>
  
  <p>I have experience in writing to txt files via PHP....</p>
  
  <p>I would like to write to a file when a form is submitted, and I have the script working to write to file " users/user_txt/$username.txt ".</p>
  
  <p>But it saves the filename as $username.txt.</p>
  
  <p>This is the code:</p>
  
  <pre><code>$fp = fopen('users/user_txt/$username.txt', 'a+');
  $fwrite = fwrite($fp, "," . $log_write);
  </code></pre>
  
  <p>This creates a blank text file and appends it, or creates file when no file exists...</p>
  
  <p>However, it saves the filename as <code>/$username.txt</code>.</p>
  
  <p>I am grabbing the variable "$username" from the form, and this echo's correct...</p>
  
  <p>But I need it to actually create the filename as:</p>
  
  <pre><code>DJ-PIMP.txt
  </code></pre>
  
  <p>If I submit my username as DJ-PIMP on the form...</p>
  
  <p>Let’s pretend I fill the form and enter username as <code>dave123</code>.</p>
  
  <p>I need the script to write to file like this:</p>
  
  <pre><code>$fp = fopen('users/user_txt/dave123.txt', 'a+');
  </code></pre>
  
  <p>How would I code this so that when I submit the form (as soon as form submitted, the variable $username would be 'dave123').</p>