在运行命令行脚本时设置 PHP 环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18098006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:58:42  来源:igfitidea点击:

Setting PHP enviromental variable while running command line script

phpcommand-lineenvironment-variablescommand-line-arguments

提问by Wiktor

I need to run a PHP script from command line and I need to set some environmental variables. Unfortunately, following does not work:

我需要从命令行运行 PHP 脚本,并且需要设置一些环境变量。不幸的是,以下不起作用:

php -dAPPLICATION_ENV=staging script.php

What I'd like to accomplish is having APPLICATION_ENVvariable set.

我想要完成的是设置APPLICATION_ENV变量。

回答by Marek

APPLICATION_ENV=staging php script.php

The variable will be available in the $_SERVER array:

该变量将在 $_SERVER 数组中可用:

echo $_SERVER['APPLICATION_ENV'];

回答by DaveRandom

There is no way to set environment variables from the command line specifically for the execution of a script by passing options to the PHP binary.

无法通过向 PHP 二进制文件传递选项来从命令行专门为执行脚本设置环境变量。

You have a few options:

您有几个选择:

  1. Set the variable globally on the system.
  2. Set the variable on the command line before calling the script. This will persist in the environment after your script has finished executing, which you may not want.
  3. Wrap the PHP script in another script, allowing you to create a temporary variable that exists only for the duration of the script.
  4. Use a command line option instead of an environment variable.
  1. 在系统上全局设置变量。
  2. 在调用脚本之前在命令行上设置变量。这将在您的脚本完成执行后保留在环境中,这是您可能不想要的。
  3. 将 PHP 脚本包装在另一个脚本中,允许您创建一个仅在脚本运行期间存在的临时变量。
  4. 使用命令行选项而不是环境变量。

The last two options are probably the cleanest way to do this, in that the variable created only exists for the run time of your script.

最后两个选项可能是最干净的方法,因为创建的变量仅在脚本运行时存在。

The implementation of option 1 is system dependent.

选项 1 的实现取决于系统。

The implementation of option 2 is also system dependent - on Windows you would do set APPLICATION_ENV=staging&& php script.phpand on *nix it would be export APPLICATION_ENV='staging' && php script.php.

选项 2 的实现也依赖于系统——你会在 Windows 上执行,set APPLICATION_ENV=staging&& php script.php而在 *nix 上则是export APPLICATION_ENV='staging' && php script.php

If you were to go for option 3 you might be tempted to go for a shell script, but this is not portable (you would need a batch file for Windows and a shell script for *nix environments. Instead, I'd suggest you write a simple PHP wrapper script, something like this:

如果您选择选项 3,您可能会尝试使用 shell 脚本,但这不是可移植的(您需要一个用于 Windows 的批处理文件和一个用于 *nix 环境的 shell 脚本。相反,我建议您编写一个简单的 PHP 包装器脚本,如下所示:

<?php

    putenv('APPLICATION_ENV=staging');

    include('script.php');

This allows you to leave your target script unchanged and set the environment variable for the script's session only.

这允许您保持目标脚本不变并仅为脚本会话设置环境变量。

A more complex wrapper script could easily be created which would allow you to specify variables on the command line, and even dynamically specify the script which should be executed when these variables are set.

可以轻松创建更复杂的包装脚本,它允许您在命令行上指定变量,甚至动态指定在设置这些变量时应执行的脚本。

Option 4 can be implemented using the $argvvariable:

选项 4 可以使用$argv变量实现:

<?php

    $applicationEnv = $argv[1];

    // rest of you script

...and call the script like:

...并调用脚本,如:

php script.php staging


However, it occurs to me that you seem to be indicating to the script which environment is running in (staging, dev, live, etc) - in which case it might be simplest to set a server-wide variable, and rename it as necessary to prevent collision with variables that other applications may be setting. That way you can simply invoke the script and not need to worry about this. This is assuming that you staging environment runs on a different machine to the live (which it should be).

但是,我突然想到您似乎在向脚本指示正在运行哪个环境(登台、开发、实时等) - 在这种情况下,设置服务器范围的变量并根据需要重命名可能是最简单的以防止与其他应用程序可能设置的变量发生冲突。这样您就可以简单地调用脚本而无需担心这一点。这是假设您的登台环境与实时环境(应该是)在不同的机器上运行。

回答by ciruvan

When you execute a PHP script from the command line, it inherits the environment variables defined in your shell. That means you can set an environment variable using the export command like so:

当您从命令行执行 PHP 脚本时,它会继承在您的 shell 中定义的环境变量。这意味着您可以使用 export 命令设置环境变量,如下所示:

export APPLICATION_ENV='staging'

回答by Abdou Tahiri

Here is an example for setting one envirnnomental variable :

这是设置一个环境变量的示例:

ENV_VAR='var' php script.php 

Just in case you want to set multiple variables Try this :

以防万一你想设置多个变量试试这个:

ENV_VAR1=1 ENV_VAR2=2 ENV_VAR3=3 php script.php 

回答by Westwick

You can set a variable in /etc/environmentlike FOO="bar"which is then accessible with getenv() in both CLI and web requests. You may need to relog for this change to take effect.

您可以在/etc/environmentlike 中设置一个变量,FOO="bar"然后可以在 CLI 和 Web 请求中使用 getenv() 访问该变量。您可能需要重新登录才能使此更改生效。

回答by Sal00m

Try using putenv and pass the variables through parameters

尝试使用 putenv 并通过参数传递变量

php script.php APPLICATION_ENV=staging

And in the script.php code:

在 script.php 代码中:

for($i=1;$i<count($argv);$i++){
     putenv($argv[$i]);
}

回答by V. Kovpak

I have the same situation and i use next code (it works for me):

我有同样的情况,我使用下一个代码(它对我有用):

export APPLICATION_ENV=staging && php script.php

Hope it will helpful for you too.

希望对你也有帮助。