Getenv() PHP Function

Using Getenv() to Retrieve an IP Address or Document Root

The getenv() function is used to retrieve the value of an environment variable in PHP. The getenv() function returns the value of a specified environment variable. The function follows the syntax getenv (varname). 

What Are Environment Variables

Environment variables are imported into the environment where the PHP code runs. You probably have more than one deployment of code: a local one for development and one in the cloud, each with different credentials. The environment variables for any two locations are different, so it makes sense not to include them in the main code.

Examples of the Getenv() Function

Below are some examples of environment variables you can use. These code examples retrieve an IP address, the admin's contact information, and the document root.



mla apa chicago
Your Citation
Bradley, Angela. "Getenv() PHP Function." ThoughtCo, Jan. 29, 2020, Bradley, Angela. (2020, January 29). Getenv() PHP Function. Retrieved from Bradley, Angela. "Getenv() PHP Function." ThoughtCo. (accessed March 30, 2023).