简体   繁体   中英

getenv return null from terminal while running php script

I defined in httpd.conf(Apache) setEnv=prod
and call to ENV in php (using getenv() function)

while running from terminal(mac) php file.php the env is NULL
but when I run it from the web page its working; and print "prod"

There are several ways to set the environment of a program you run in the console (Terminal) in macOS and Linux. (The procedures are similar on Windows but the syntax is different.)

You can put env=prod in front of the command line to set the environment variable env with value prod only for the current execution of that command line:

$ env=prod php -f script.php

You can use export env=prod as a separate command line. It sets the environment value env with value prod for the current execution of the shell you are running in the console. The export keyword in front of it tells the shell to pass the variable to all processes it launches from now on (until you close the shell or unset the variable):

$ export env=prod
$ php -f script.php

If you want to set the variable permanently then you put the line export env=prod in ~/.profile or ~/.bashrc or the initialization script of your preferred shell (if it's not bash ). All subsequent instances of the shell you launch execute the initialization script and set the env variable with value prod in their environment and mark it as exportable to the child processes that shell launches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM