getenv() vs. $_ENV in PHP
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 checkisset
/empty
before access.getenv()
won't emit notices.
Why is my $_ENV empty?
Turns out there was two issues here:
1. $_ENV
is only populated if php.ini allows it, which it doesn't seem to do by default, at least not in the default WAMP server installation.
; This directive determines which super global arrays are registered when PHP
; starts up. If the register_globals directive is enabled, it also determines
; what order variables are populated into the global space. G,P,C,E & S are
; abbreviations for the following respective super globals: GET, POST, COOKIE,
; ENV and SERVER. There is a performance penalty paid for the registration of
; these arrays and because ENV is not as commonly used as the others, ENV is
; is not recommended on productions servers. You can still get access to
; the environment variables through getenv() should you need to.
; Default Value: "EGPCS"
; Development Value: "GPCS"
; Production Value: "GPCS";
; http://php.net/variables-order
variables_order = "GPCS"
When I set the variables_order
back to EGPCS
, $_ENV
is no longer empty.
2. When you use SetEnv
in your .htaccess
, it ends up in $_SERVER
, not in $_ENV
, which I gotta say is a tad confusing when it's named SetEnv
...
# .htaccess
SetEnv ENV dev
SetEnv BASE /ssl/
# php
var_dump($_SERVER['ENV'], $_SERVER['BASE']);
// string 'dev' (length=3)
// string '/ssl/' (length=5)
3. The getenv
function will always work and is not affected by the PHP setting for $_ENV Additionally it seems to do so insensitive to case, which might be useful.
var_dump(getenv('os'), getenv('env'));
// string 'Windows_NT' (length=10)
// string 'dev' (length=3)
How to Loads environment variables from .env to getenv() PHP MVC
According to the documentation, you should use Dotenv::createUnsafeImmutable()
So instead of:
$dotenv = Dotenv\Dotenv::createMutable(BASE_PATH);
Use:
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(BASE_PATH);
PHP: difference between getenv() and apache_getenv()
getenv
calls apache_getenv
if you're running the Apache SAPI, otherwise it asks the system. So, no, there is no real functional difference. Stick with getenv
.
how can I access environment vars in php
You can access env variables through this function:
getenv ( string $varname )
So, if for example you want the database name:
$db = getenv('DATABASE');
Documentation:
http://php.net/manual/en/function.getenv.php
Related Topics
PHP File Uploads Doesnot Read $_Files['Image']
How to Create a Logfile in PHP
Friend of a Friend in PHP/Mysql
How to Trigger Xdebug Profiler for a Command Line PHP Script
How to Write to the Console from a Laravel Controller
Ajax/PHP - How to Get Posted Data to Load into a Modal on the Same Page
Php: How to Get a List of Classes That Implement Certain Interface
Most Efficient Way to Do Language File in PHP
How to Post Soap Request from PHP
Windows 7 PHP + Symfony2 Terribly Slow
Warning: Cannot Modify Header Information - Headers Already Sent
Passing Value from PHP Script to Python Script
How to Validate on Max File Size in Laravel
What Is Difference Between PHP Cli and PHP Cgi