- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
$_ENV is another superglobal associative array in PHP. It stores environment variables available to current script. $HTTP_ENV_VARS also contains the same information, but is not a superglobal, and now been deprecated.
Environment variables are imported into global namespace. Most of these variables are provided by the shell under which PHP parser is running. Hence, list of environment variables may be different on different platforms.
This array also includes CGI variables in case whether PHP is running as a server module orCGI processor.
PHP library has getenv()function to retrieve list of all environment variables or value of a specific environment variable
Following script displays values of all available environment variables
<?php $arr=getenv(); foreach ($arr as $key=>$val) echo "$key=>$val "; ?>
To obtain value of specific variable use its name as argument for getenv() function
<?php echo "Path: " . getenv("PATH"); ?>
Browser will display result similar to following
PHP also has putenv() function to create a new environment variable. The environment variable will only exist for the duration of the current request.
Changing value of certain environment variables should be avoided. By default, users will only be able to set environment variables that begin with PHP_ (e.g. PHP_FOO=BAR).
The safe_mode_protected_env_vars directive in php.ini contains a comma-delimited list of environment variables, that the end user won't be able to change using putenv().
<?php putenv("PHP_TEMPUSER=GUEST"); echo "Temp user: " . getenv("PHP_TEMPUSER"); ?>
Browser will display result as following
Temp user: GUEST
- PHP php://
- How to deal with error “Error in eval(predvars, data, env) : numeric 'envir' arg not of length one” in R?
- PHP Generators.
- PHP Overloading
- PHP Traits
- PHP $_GET
- PHP $GLOBALS
- PHP $_SERVER
- PHP superglobals
- PHP References
- PHP Constants
- PHP Expressions
- PHP Tags
- PHP Iterables
- PHP NULL
- PHP Objects.