Accessing Environment Variables3:50 with Alena Holligan
Environment variable changes based on the current system. There are a number of environment variables that are set by default, which we can access right away. We'll look at what they are and how we can use them.
Learn About Your Environment
phpinfo allows us to gather a lot of information about our environment. Among these details, we can see the version of PHP that is being run and the default environment variable that are set. Check the section for "Apache Environment" and "Additional Modules".
getenv — Gets the value of an environment variable.
string getenv ( string $varname [, bool $local_only = FALSE ] )
Welcome back to our instructional environment. 0:00 We're ready to combine our knowledge of environments with 0:03 our knowledge of variables. 0:06 A variable allows us to store information. 0:07 And the environment is a system in which our code is run. 0:10 An environment variable changes based on which system we're running. 0:13 Because of this, an environment variable is not stored in our code base. 0:17 Instead, it is stored in some sort of configuration file on the system. 0:22 This also means that these environment variables 0:27 are not specific to a programming language. 0:29 They can be used by any program, 0:32 regardless of language, running on that particular environment. 0:34 There are a number of environment variables that are set by default, 0:39 which means we can access them right away. 0:43 Let's jump into Workspaces and see what we have to start with. 0:45 Let's create a file named phpinfo.php. 0:49 And we're going to echo the function phpinfo. 0:57 Now let's take a look at this in our browser. 1:01 This gives us a lot of information about our environment, 1:06 including which version of PHP is running. 1:09 If we scroll down, we'll also see a section on Apache Environment. 1:14 These are the environment variables we're given by default. 1:19 They include such things as HTTP_HOST, DOCUMENT_ROOT, and SCRIPT_NAME. 1:23 Notice that all these variables are uppercase. 1:30 Although this is not a requirement, we'll want to follow this convention 1:35 when adding our own environment variables later on. 1:39 When we scroll down near the bottom, we see under 1:43 Additional Modules, another Environment. 1:48 These are accessible as environment variables as well. 1:52 You may also notice that usually the same Apache Environment variables 1:55 are available as server variables as well. 1:59 We're going to be accessing all environment variables in the same way. 2:03 Let's go back to workspaces. 2:08 To access an environment variable, we use the function getenv and 2:10 we pass the name of the environment variable we wish to use. 2:15 We'll use SCRIPT_NAME. 2:18 And let's var dump this function. 2:23 Now we can go back to the browser to see the results And 2:27 here is our environment variable. 2:34 Let me show you one more thing. 2:37 I want you to keep in mind that this is a function and 2:40 not a direct call to a variable. 2:44 So if the variable does not exist, let's change this to BLAH, 2:46 the getenv function will return false. 2:51 If we directly called a nonexistent variable,we'd get an error. 2:58 But instead we get false. 3:02 Another major advantage to having these variables stored outside your code 3:05 is that you do not have to worry about them getting added 3:10 to a version control system such as GitHub. 3:12 This is a major concern when it comes to things like connection credentials. 3:15 You don't want to expose passwords on GitHub. 3:20 Storing environment variables outside of your code also allows you to limit 3:24 the number of users who have direct access to your production credentials. 3:29 I don't need to know the contents of the variable 3:33 in order to be able to use that variable in my code. 3:36 In the next video, we'll start working with our own environment variables, in 3:41 order to secure our credentials and allow our code to adapt to its environment. 3:45
You need to sign up for Treehouse in order to download course files.Sign up