Windows Git Bash does not recognize commands in PHP exec - php

I have a simple test PHP file:
<?php echo exec('php -v');
When I execute php -v in Git Bash, the console displays:
PHP 5.6.3 (cli) (built: Nov 12 2014 17:18:08)
Copyright (c) 1997-20147 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
But, when I execute php test.php the console displays:
‘php’ is not recognized as an internal or external command, operable program or batch file.
If I execute php test.php in cmd.exe this works and displays:
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
Note: I have Windows 8.1 and Git Bash 2.7.1.2
SOLVED
I had deleted folders in my PATH var, this isn't a problem for cmd.exe but, in Git Bash I don't know why doesn't work.
I deleted the deleted paths from PATH and it works.

Re-install Git, and make sure to tick the Run Git from the Windows command prompt option.
Also, if Git is freshly installed, you might need to restart your PC for the new PATH variable to be recognized (the way "php" is globally recognized rather than setting a path to the php actual executable).

Related

Running gulp in docksal

I have a Drupal 9 (this isn't a Drupal question) site installed in a Docksal container. In its theme I have node.js and gulp.
I seem to have a Catch-22.
From the command line:
$ gulp
Pattern Lab Node v5.4.0
Pattern Engine mustache: good to go
Error: php cli required. Command failed: /bin/sh -c php --version
/bin/sh: php: command not found
So I go to the bash shell in Docksal, because I know that from there I can, for instance, do this:
$ php -v
PHP 8.1.4 (cli) (built: Mar 29 2022 02:03:50) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.4, Copyright (c) Zend Technologies
with Zend OPcache v8.1.4, Copyright (c), by Zend Technologies
with blackfire v1.76.0~linux-x64-non_zts81, https://blackfire.io, by Blackfire
that is, run php from the command line.
When I try running the gulp command there:
$ gulp
bash: gulp: command not found
I don't think I should have to run it from fin bash. I've been in other environments where gulp runs quite nicely from the command line in both docksal and lando, and the php command invokes php. I don't know what I'm missing here.

Trying to run php file in cmd, without using xampp, however it's not working

I run the directory folder of the code that I'm about to run, then I used the code php -S localhost:3000, I also tried php -S localhost:8000 and php -S 0.0.0.0:8080, however none of these codes seems to work.
C:\Users\Documents\fisiko\v2-boston-brain-science> php -S localhost:3000
'php' is not recognized as an internal or external command,
operable program or batch file
why is this happening??
When this issue has happened to me in the past, is most likely because of php is not been recognized by Windows. I've had to add manually the path to where is PHP C:\xampp\php to the system properties, not sure if this was already added.
Restart your machine, the new value should be loaded in the variable Path in the system. Now php should be recognizable. Typing php -v in the console should be return something like:
PHP 7.4.1 (cli) (built: Dec 17 2019 19:24:02) ( ZTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
Should be good after, hope this helps.

PHP --version shown incorrectly on osX

I've installed in my osX php7 from homebrew, however when I type in the shell eitherphp --version or php-fpm --version I get as result an older version
PHP 5.5.30 (fpm-fcgi) (built: Oct 23 2015 17:22:03)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
From the shell I looked for every php file or folder sudo find / -name "php" but nothing linked to PHP 5.5
What should I do?
After creating ~/.bash.profile as written in comments by Michael, I found the solution thanks to change the PHP path to MAMPs PHP
In ~/.bash_profile I wrote:
export PATH=/usr/local/Cellar/php70/7.0.2/bin:$PATH
export PATH=/usr/local/Cellar/php70/7.0.2/sbin:$PATH
First row is for php, the second for php-fpm
Then, to be sure that changes take effect type in terminal source ~/.bash_profile

Use different PHP version CLI executable for one command

So I have Gentoo box with three PHP versions installed (nevermind the reasons):
/usr/bin/php -> /usr/lib64/php5.4/bin/php
/usr/bin/php5.5 -> /usr/lib64/php5.5/bin/php
/usr/bin/php5.6 -> /usr/lib64/php5.4/bin/php
I want to install Laravel framework using composer:
$ composer create-project laravel/laravel --prefer-dist
This however throws an error because Laravel requires PHP > 5.5.9 and the default php interpreter is 5.4.
So I issue another command:
$ /usr/bin/php5.6 /usr/bin/composer create-project laravel/laravel --prefer-dist
This takes me one step further, but then some post-install commands from Laravel's composer.json comes into play, and installation crashes.
This is due to the fact, that composer.json commands look like this:
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
As you can see, the "default" interpreter is used again!
Now, proper PHP files start with following shebang:
#!/usr/bin/env php
This is nice feature as PHP interpreter can be found under different locations on different systems.
Unfortunatelly, in this case env command returns path to the first executable it finds in $PATH environmental variable.
How could I possibly alter current session environment or what kind of trick to perform so for the execution of whole Laravel installation process php command would invoke /usr/bin/php5.6 instead of /usr/bin/php?
I don't want to change $PATH variable or modify files like composer, composer.json or Laravel's CLI utility artisan.
Edit: also assume that I want to do this from regular user account (i.e. with no root permissions).
Default PHP executable can be found using:
$ which php
In most cases it is link to particular PHP version:
lrwxrwxrwx 1 root root 21 aug 15 2016 /usr/bin/php -> /usr/bin/php7.1
To change it to different version just relink it to another
$ sudo rm /usr/bin/php
$ sudo ln -s /usr/bin/php5.6 /usr/bin/php
Before relink you have to make sure target PHP version is installed.
Maybe you can try to fix the environnement!
$ php -v
PHP 5.4.x (cli) ...
$ set PATH="/usr/lib64/php5.6/bin:$PATH"
$ php -v
PHP 5.6.x (cli) ...
Or, if you don't want to modify the PATH for your shell session, you can scope the change for the current command only:
$ php -v
PHP 5.4.x (cli) ...
$ env PATH="/usr/lib64/php5.6/bin:$PATH" php -v
PHP 5.6.x (cli) ...
$ php -v
PHP 5.4.x (cli) ...
Identify where the current generic php command is and to which binary it points to with which php.
It will give you a path to a symlink like you mention in your question
/usr/bin/php -> /usr/lib64/php5.4/bin/php
Edit the symlink to point to which ever php version you want for now, see here
https://unix.stackexchange.com/questions/88824/how-can-i-edit-symlinks
When you are done just reverse the process.
UPDATE:
you can also add an alias for the current user by editing ~/.bashrc and adding the following
alias php='/usr/bin/php5.6'
see if this works out
Since PHP7 came around Debian Linux creates different executables for PHP versions 5 and 7 in /usr/bin by default (if you install both versions that is).
Calling those different versions from the command line is as simple as ever now:
kkarski#debian:~ $ php5 -v
PHP 5.6.26-0+deb8u1 (cli) (built: Sep 21 2016 12:37:50)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
kkarski#debian:~ $ php -v
PHP 7.0.9-1~dotdeb+8.1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.9-1~dotdeb+8.1, Copyright (c) 1999-2016, by Zend Technologies
This is obviously only good for simple scripts. For anything larger (composer, artisan etc.) you'll have to change the PATH variable.
To change the version your Apache server is using all you have to do is:
root#debian:~# a2dismod php5 && a2enmod php7.0
Module php5 disabled.
To activate the new configuration, you need to run:
service apache2 restart
Considering conflict php5 for php7.0:
Enabling module php7.0.
To activate the new configuration, you need to run:
service apache2 restart
and vice versa if you want to use the lower PHP version.
Mentioning it in case someone has similar problems on Debian.
I find the easiest to achieve the same like just create a softlink like for example
ln -s /opt/php-7.0.32/bin/php /usr/bin/php7
ln -s /opt/php-7.1/bin/php /usr/bin/php71
ln -s /opt/php-5.6/bin/php /usr/bin/php56
then as you use your default version say it is php7.2 as just php for alternative version you can you php7 or php71 or php56
here ln -s /opt/php-7.1/bin/php /usr/bin/php71 is the source/orginal file and /usr/bin/php7 is the destination / link
For anyone else who found no solution in the above, because they use composer update and somehow the wrong PHP version gets used. By using composer self-update I got some more info and eventually found out that in the composer.json you can specify a platform in the config section, which overrides what php version is used by composer. Simply changing this value or removing this config solved my issue.
composer.json
"config": {
"platform": {
"php": "7.1"
},
It's possible to do using alias, but keep in mind that aliases are not expanded by default.
You must also enable expanding of those.
$ shopt -s expand_aliases
$ alias php="/usr/local/bin/php-5.6"
$ ./some-script.sh
$ unalias php # back to previous version
I worked on a "script + docker image" to make multiple php versions available whenever I want during development: https://github.com/jclaveau/docker-php-multiversion
You can use it this way:
$ php 5.6 -v
PHP 5.6.40-15+ubuntu18.04.1+deb.sury.org+1 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
$ php 7.3 -v
PHP 7.3.13-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Dec 18 2019 14:48:49) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.13, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.13-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v2.9.0, Copyright (c) 2002-2019, by Derick Rethans
As it uses Docker.io, you do not need to alter any part of your system configuration.
Hoping it would help you

How to tell /bin/bash the correct php version? (Mavericks)

I am using SublimeText to run PHPUnit tests continuosly. Works great, but now I stumbled upon something strange. I updated to the latest PHP version via Homebrew and if I open a Terminal session and type php -v, this is what I get:
PHP 5.6.7 (cli) (built: Mar 22 2015 19:03:55)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
The shell instance that is used by SublimeText seems not to use this version of PHP. If I do the same there (via writing echo shell_exec('php -v');die(); into a PHPUnit test), I get this:
PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
I tested around a bit and found out, that automator puts out the same if I run a shell script in \bin\bash shell via automator's 'run shell script' action.
How to tell /bin/bash to use the newly installed PHP version?
When you issue the command $ php, the PATH is searched for the binary. You can find out which one will be executed with the command $ which php.
If you want to explicitly run one or another php binary when multiple ones are installed, use full absolute path to the binary, e.g. $ /opt/php5.6/bin/php.

Categories