dongtun1872 2011-03-22 16:27
浏览 60
已采纳

在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 2011-03-22 19:30
    关注

    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 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件