Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current PHP executable from within a script?

Tags:

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

php5 outer.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 isn't any such variable in PHP.

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

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.

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

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). I am falling back to using:

$version = explode('.', phpversion()); $phpcli = "php{$version[0]}"; 
like image 388
benizi Avatar asked Mar 03 '10 15:03

benizi


People also ask

Where do I find PHP EXE?

Php.exe is located in a subfolder of the user's profile folder —common is C:\Users\USERNAME\AppData\Local\php7\. Known file sizes on Windows 10/8/7/XP are 28,739 bytes (57% of all occurrences) or 106,496 bytes. The program has a visible window. It is not a Windows system file.

Where is PHP executable Linux?

/usr/bin/php7. 0.

Can you make an EXE with PHP?

Bambalam PHP EXE Compiler/Embedder is a free command line tool to convert PHP applications to standalone Windows .exe applications. The exe files produced are totally standalone, no need for php dlls etc.

Where can I find PHP in Ubuntu?

The default location for the php. ini file is: Ubuntu 16.04: /etc/php/7.0/apache2.


1 Answers

It is worth noting that now in PHP 5.4+ you can use the predefined constant PHP_BINARY:

PHP_BINARY

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

like image 144
Kristopher Ives Avatar answered Sep 20 '22 16:09

Kristopher Ives