dongtun1872
dongtun1872
2011-03-22 16:27

在PHP中设置环境变量以便在Windows批处理命令中进一步使用

已采纳

The story is like this:

  • After running a JUnit test campaign, I run a php script to get the variables $testsOK and $testsFailed.

  • In the same php script I have to add to environment variables "OK" and "FAILED" the numbers that I obtained just before in the same script. I used putenv, $_ENV["OK"]= ... even apache_setenv("OK",$testsOK), which of course does not work since I do not have a webserver, I only use PHP CLI.

  • I will have to use these environment variables "OK" and "FAILED" in windows batch command after the script was run.

On the putenv page on php.net (http://php.net/manual/en/function.putenv.php) someone says that: "putenv/getenv, $_ENV, and phpinfo(INFO_ENVIRONMENT) are three completely distinct environment stores. doing putenv("x=y") does not affect $_ENV; but also doing $_ENV["x"]="y" likewise does not affect getenv("x"). And neither affect what is returned in phpinfo()."

Therefore, none of these methods will help me. My question is: should I drop this method and try with another language? :-) or is there a way that I'm missing.

I would very much appreciate your help. Anca

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douzhuangna6906 douzhuangna6906 10年前

    You may wrap the invocation in a script, the PHP would return via stdout the values to be set; the script would then capture this output into the appropiate environment variables.

    A very simplified version would be something like this.

    @echo off
    runtestcampaign
    for /f %%a in ('php myscript.php') do (
      set OK=%%a
      set Fail=%%b
    )
    restofbat 
    
    点赞 评论 复制链接分享