0

I have multiple PHPs (8.1 and 7.3). If I execute php -v then I get 7.3.

I need to execute some command with PHP for magento 2 e.g. php bin/magento cache:flush, but I need to call it with php 8.1 for a certain folder (/var/www/company) but I don't want to always have to specify my php 8 binary e.g. php8.1 bin/magento cache:flush to make it work.

Is there a way to make it always use PHP 8.1 IF I am in that specific directory?

Black
  • 2,079

1 Answers1

2

This is a quick and dirty wrapper function you can use in a shell like sh or bash:

php () (
   case "$PWD/" in
      /var/www/company/ ) c=php8.1 ;;
      *                 ) c=php    ;;
   esac
   exec "$c" "$@"
)

Now it's enough to run php whatever … and one or the other executable will be chosen accordingly. You can confirm this by trying php -v in /var/www/company and elsewhere.

Notes:

  • You may want /var/www/company/* instead of /var/www/company/.
  • $PWD holds what pwd -L would print (see the beginning of this answer). You may want what pwd -P would print; if this is the case, use case "$(pwd -P)/" in.
  • The whole function runs in a subshell, so c is "local" without any non-portable local c. Ultimately the subshell is replaced by php or php8.1 executable thanks to exec.