Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getenv() vs. $_ENV in PHP

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

Any trade-offs between using either?

I noticed sometimes getenv() gives me what I need, while $_ENV does not (such as HOME).

like image 805
tau Avatar asked Jan 10 '12 03:01

tau


People also ask

What does Getenv do in PHP?

getenv() function returns the list of built-in environment variables of the PHP. But if the coder needs to create any new environment variable for the programming purpose, they can do so. putenv() function can be used to create a new environment variable with a value.

Can I use .env in PHP?

An . env file is a plain text file which contains environment variables definitions which are designed so your PHP application will parse them, bypassing the Apache, NGINX and PHP-FPM. The usage of . env files is popular in many PHP frameworks such as Laravel which has built-in support for parsing .

Is Getenv thread safe?

getenv need not be thread-safe; it is a read-only function and will not break your code.

Should you use .env in production?

Using environment variables is a somewhat common practice during Development but it is actually not a healthy practice to use with Production. While there are several reasons for this, one of the main reasons is that using environment variables can cause unexpected persistence of variable values.


2 Answers

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 when running on case-insensitive file systems (like Windows). On Linux hosts it still works as case-sensitive. 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 to improve cross-platform behavior, unless you are certain about the casing of the environment variable you are trying to retrieve.

Steve Clay's comment in this answer highlights another difference:

Added getenv() advantage: you don't need to check isset/empty before access. getenv() won't emit notices.

like image 70
Batkins Avatar answered Nov 01 '22 17:11

Batkins


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.
like image 44
Conor McDermottroe Avatar answered Nov 01 '22 18:11

Conor McDermottroe