|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52072 RFE: can we have `which`
Submitted: 2010-06-13 02:34 UTC Modified: 2010-06-13 03:12 UTC
Avg. Score:2.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: php at richardneill dot org Assigned:
Status: Wont fix Package: Filesystem function related
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: php at richardneill dot org
New email:
PHP Version: OS:


 [2010-06-13 02:34 UTC] php at richardneill dot org
It would be nice if PHP had a builtin "which" command.

For example,  which("ffmpeg")  would allow the user to check whether ffmpeg 
was installed, prior to calling it with exec().

Test script:
which("ffmpeg") should return eg:
if the command exists, and is in the £PATH for exec(), or
if it doesn't exist.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-13 02:47 UTC]
-Status: Open +Status: Wont fix
 [2010-06-13 02:47 UTC]
This seems trivial to do in userspace to me:

function which($cmd) {
	$paths = explode(':',$_ENV['PATH']);
	foreach($paths as $path) {
		$p = $path.'/'.$cmd;
		if(file_exists($p)) return $p;
	return false;
 [2010-06-13 03:06 UTC] php at richardneill dot org
Thanks for your quick reply. I agree - it's trivial to do in userspace. 

The simplest way is just to 
 $lastline = exec ("which $ffmpeg", $output, $retval)
 if (retval){
   return ($lastline)
   return ($false)

Indeed many of the PHP functions are trivial in that sense - one could build file_get_contents() out of fopen(),file(),fclose(), or could avoid using unlink by a call to system("rm filename").

However the attraction of PHP is that so many of the required functions already exist and I don't have to write them. So I think that which() would be a useful addition. 

I'm particularly thinking of the cases where php-cli is an improvement on shell-scripting.
 [2010-06-13 03:12 UTC]
Using exec is definitely not the best way to do that one.  Anytime you fork+exec a 
new process you take on a significant performance penalty.  Same goes for your 
system() example.  But, if you can do it in a couple of lines without resorting to 
an exec(), then it is indeed trivial.  file_get_contents() is slightly more 
complicated because of the streams support in it.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 22 16:01:29 2024 UTC