getenv() vs. $_ENV in PHP

后端 未结 7 1290
半阙折子戏
半阙折子戏 2020-12-05 02:00

What is the difference between getenv() and $_ENV?

Any trade-offs between using either?

I noticed sometimes getenv() g

相关标签:
7条回答
  • 2020-12-05 02:09

    I know that the comment in the docs says that getenv is case-insensitive, but that's not the behaviour I'm seeing:

    > env FOO=bar php -r 'print getenv("FOO") . "\n";'
    bar
    > env FOO=bar php -r 'print getenv("foo") . "\n";'
    
    > env foo=bar php -r 'print getenv("foo") . "\n";'
    bar
    > env foo=bar php -r 'print getenv("FOO") . "\n";'
    
    > php --version
    PHP 5.4.24 (cli) (built: Jan 24 2014 03:51:25)
    Copyright (c) 1997-2013 The PHP Group
    Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    

    Looking at the source code for the getenv function, this is because there are three ways that PHP can fetch the environment variable:

    1. Via sapi_getenv (e.g. if it's getting the environment variable from Apache)
    2. If on Windows, from GetEnvironmentVariableA.
    3. If on non-Windows, from the getenv function provided by libc.

    As far as I can tell, the only time when it will behave in a case-insensitive manner is on Windows because that's how the Windows environment variable API behaves. If you're on Linux, BSD, Mac, etc then getenv is still case sensitive.

    As mentioned by mario, $_ENV is not always populated due to different configurations of variables_order so it's best if you avoid $_ENV if you don't control the server configuration.

    So, for the most portable PHP code:

    1. Use getenv.
    2. Use the correct case for the environment variable name.
    0 讨论(0)
  • 2020-12-05 02:12

    Additionally $_ENV is typically empty if variables_order does't have E listed. On many setups it's likely that only $_SERVER is populated, and $_ENV is strictly for CLI usage.

    On the other hand getenv() accesses the environment directly.

    (Regarding the case-ambiguity, one could more simply employ array_change_key_case().)

    0 讨论(0)
  • 2020-12-05 02:13

    I found getenv() useful to avoid a strange PHP bug where sometimes $_SERVER and $_ENV was undefined if auto_globals_jit was enabled (creating the _SERVER and _ENV variables when they're first used). Since then I began to to use it.

    0 讨论(0)
  • 2020-12-05 02:14

    I'd add that getenv() is a better choice because, as a function, it can be overloaded for testing purposes. Whereas overwriting your $_SERVER or $_ENV variables might interfere with test frameworks and other libraries and ultimately require a lot more work to carry out safely.

    0 讨论(0)
  • 2020-12-05 02:16

    According to the php documentation about getenv, they are exactly the same, except that getenv will look for the variable in a case-insensitive manner. Most of the time it probably doesn't matter, but one of the comments on the documentation explains:

    For example on Windows $_SERVER['Path'] is like you see, with the first letter capitalized, not 'PATH' as you might expect.

    Because of that, I would probably opt to use getenv unless you are certain about the casing of the title of the variable you are trying to retrieve.

    0 讨论(0)
  • 2020-12-05 02:19

    Taken from the PHP docs:

    This function is useful (compared to $_SERVER, $_ENV) because it searches $varname key in those array case-insensitive manner. For example on Windows $_SERVER['Path'] is like you see Capitalized, not 'PATH' as you expected. So just: <?php getenv('path') ?>

    0 讨论(0)
提交回复
热议问题