I am on Windows 10 and have the following PHP code:
<?php
system("php -v");
?>
It is expected to output my PHP version as it does when I run php -v
in my command prompt:
PHP 8.1.6 (cli) (built: May 11 2022 08:55:59) (ZTS Visual C 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.1.6, Copyright (c) Zend Technologies
However, it is not giving any output at all.
Can someone guide me on how to fix this? Other commands such as python --version
are returning the appropriate output through the system
function, only php -v
has this behavior.
UPDATE: I just tried system(".\~\..\..\xampp\php\php.exe -v");
so it uses the exact path of the PHP executable but still no luck. Again, running the same command in the command prompt returns the desired output for some reason.
UPDATE 2: Trying the update suggested by @hanshenrik seems to generate the following error: 'php' is not recognized as an internal or external command, operable program or batch file. string(31) "operable program or batch file."
. I do have PHP in my path and using php -v
in a normal command prompt in any directory gives me the relevant results, it's just that the system
or (recently tested) passthru
commands in PHP are not getting it to execute.
CodePudding user response:
seems like a "PATH environment variable problem" for unknown reasons, your PHP's PATH does not include C:\xampp\php
, while your cmd's PATH does include it.
you can ensure it's there at runtime by doing
setenv("PATH",getenv("PATH").PATH_SEPARATOR."C:\\xampp\\php");
this is a simple approach and should work in the real world, but is kind-of non-optimal because it risks duplicating the path. a "proper" solution to adding the path would probably be something like (untested)
if(!in_array("C:\\xampp\\php",explode(PATH_SEPARATOR, getenv("PATH")), true){
setenv("PATH",getenv("PATH").PATH_SEPARATOR."C:\\xampp\\php");
}