title: How to find user's home directory on Windows and Linux in PHP
body: <p>I've understood there are several ways to determine the user's home, depending on the platform (mainly Unix/Linux vs Windows).</p>
  
  <p>Composer uses an environment variable, in <a href="https://github.com/composer/composer/blob/ef46a8afa4f5d845befbc7be832432c4b30d6313/src/Composer/Util/Platform.php#L48" rel="nofollow noreferrer">composer/Platform</a> package:</p>
  
  <pre><code>public static function getUserDirectory()
  {
      if (false !== ($home = getenv('HOME'))) {
          return $home;
      }
      if (self::isWindows() &amp;&amp; false !== ($home = getenv('USERPROFILE'))) {
          return $home;
      }
      if (function_exists('posix_getuid') &amp;&amp; function_exists('posix_getpwuid')) {
          $info = posix_getpwuid(posix_getuid());
          return $info['dir'];
      }
      throw new \RuntimeException('Could not determine user directory');
  }
  
  public static function isWindows()
  {
      return defined('PHP_WINDOWS_VERSION_BUILD');
  }
  </code></pre>
  
  <p><a href="https://github.com/webmozart/path-util/blob/95a8f7ad150c2a3773ff3c3d04f557a24c99cfd2/src/Path.php#L245" rel="nofollow noreferrer">Webmozart's path-util</a> package uses other environment variables:</p>
  
  <pre><code>public static function getHomeDirectory()
  {
      // For UNIX support
      if (getenv('HOME')) {
          return static::canonicalize(getenv('HOME'));
      }
      // For &gt;= Windows8 support
      if (getenv('HOMEDRIVE') &amp;&amp; getenv('HOMEPATH')) {
          return static::canonicalize(getenv('HOMEDRIVE').getenv('HOMEPATH'));
      }
      throw new RuntimeException("Your environment or operation system isn't supported");
  }
  </code></pre>
  
  <p>What is the difference between these two methods? Is one more reliable than the other?
  Note: I'm using PHP in the CLI, so it's always the actual current user running PHP.</p>
  
  <p>EDIT> I understand that this question seems to ask for an opinion, but it's not the case. I DO NOT KNOW Windows and do not understand why some packages use different ways to determine the user's home directory. I'm asking for explanations about the two mentioned methods: is one of them more reliable than the other and why?
  I've edited the title and the question to reflect this precision.</p>