Getting the current interpreter in PHP


Recently a friend of mine asked if there is a way to dynamically find out the path of the PHP executable. The working scenario was: running some scripts (from the command line) which would spawn other scripts and rather than hardcoding the path to the PHP interpreter he wanted to use the same interpreter when launching the child script as the one used to launch the parent script.

PHP removes itself from the $argc variable. I also digged around in the $_SERVER and $_ENV variable to no avail. I also learned a new function: get_defined_constants. However what I did not find was an easy solution.

Under Linux you could probably play around with the /proc/self pseudo-filesystem, however this was under Windows. Finally I came up with the following (somewhat) controverted solution:

Using the win32ps extension I list the information about all the processes, then I use getmypid to find out the pid of the current process and look it up in the list.


Leave a Reply

Your email address will not be published.