Back to Top

Thursday, June 05, 2008

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.


Post a Comment

You can use some HTML tags, such as <b>, <i>, <a>. Comments are moderated, so there will be a delay until the comment appears. However if you comment, I follow.