title: PHP function to use password_hash, hash or MD5 depending on availability
body: <p>I'm creating a script and would ideally like to convert user password into a hash using password_hash function, however I want to make the hashing dynamic to make sure password_hash function actually exists, so i've written the following function to return a hash depending on what function is available. </p>
  
  <pre><code>function generateHash($hashpwd){
      if (function_exists('password_hash')) {
          return password_hash($hashpwd, PASSWORD_DEFAULT);
      } elseif (function_exists('hash')) {
          return hash('sha512', $hashpwd);
      } else {
          return md5($hashpwd);
      }
  }
  </code></pre>
  
  <p>Is there anything wrong with this method or will it work? also I am creating the hash using password_hash in the following way:</p>
  
  <pre><code>password_hash($hashpwd, PASSWORD_DEFAULT);
  </code></pre>
  
  <p>But most places i've seen example syntax in the following format:</p>
  
  <pre><code>password_hash($hashpwd, PASSWORD_DEFAULT)."\n";
  </code></pre>
  
  <p>Difference being the \n is the \n needed and what is it for?</p>