从脚本中获取当前的PHP可执行文件? [英] Get current PHP executable from within script?

查看:112
本文介绍了从脚本中获取当前的PHP可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从PHP cli中运行一个PHP cli程序.在将运行此命令的某些计算机上,同时安装了php4和php5.如果我将外部程序运行为

I want to run a PHP cli program from within PHP cli. On some machines where this will run, both php4 and php5 are installed. If I run the outer program as

php5 outer.php

我希望内部脚本以相同的php版本运行.在Perl中,我将使用$^X来获取perl可执行文件.看来PHP中没有这样的变量?

I want the inner script to be run with the same php version. In Perl, I would use $^X to get the perl executable. It appears there's no such variable in PHP?

现在,我正在使用$_SERVER['_'],因为bash(和zsh)将环境变量$_设置为上次运行的程序.但是,我宁愿不依赖于特定于shell的习惯用法.

Right now, I'm using $_SERVER['_'], because bash (and zsh) set the environment variable $_ to the last-run program. But, I'd rather not rely on a shell-specific idiom.

更新:版本差异不过是一个问题.例如,如果PHP不在PATH中,或者不是PATH中没有找到第一个版本,那么查找版本信息的建议将无济于事.

UPDATE: Version differences are but one problem. If PHP isn't in PATH, for example, or isn't the first version found in PATH, the suggestions to find the version information won't help.

此外,csh和变体似乎未为其进程设置$_环境变量,因此,解决方法不适用于该环境.

Additionally, csh and variants appear to not set the $_ environment variable for their processes, so the workaround isn't applicable there.

更新2:我使用的是$_SERVER['_'],直到我发现它在xargs下没有做正确的事情(这很有意义... zsh将其设置为命令运行,它是xargs,而不是php5,并且xargs不会更改变量).退回使用:

UPDATE 2: I was using $_SERVER['_'], until I discovered that it doesn't do the right thing under xargs (which makes sense... zsh sets it to the command it ran, which is xargs, not php5, and xargs doesn't change the variable). Falling back to using:

$version = explode('.', phpversion());
$phpcli = "php{$version[0]}";

推荐答案

值得一提的是,现在在PHP 5.4+中,您可以使用预定义的常量-

Worth noting that now in PHP 5.4+ you can use the predefined constant - PHP_BINARY:

PHP_BINARY

PHP_BINARY

指定脚本执行过程中的PHP二进制路径.自PHP 5.4起可用.

Specifies the PHP binary path during script execution. Available since PHP 5.4.

这篇关于从脚本中获取当前的PHP可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆