PHP Parse error: syntax error, unexpected ‘|’, expecting variable

I’m trying to switch PHP version from 8.1 to 7.1 and run composer install. Here’s my method:-

cd /etc/php && ls -la
drwxr-xr-x   4 root root  4096 Jan 27 09:18 .
drwxr-xr-x 141 root root 12288 Jan 27 09:18 ..
drwxr-xr-x   5 root root  4096 Jan 27 09:18 7.1
drwxr-xr-x   4 root root  4096 Dec  5 20:21 8.1

php -v
PHP 8.1.2-1ubuntu2.10 (cli) (built: Jan 16 2023 15:19:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.10, Copyright (c), by Zend Technologies
    with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans

composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ / __ `__ / __ / __ / ___/ _ / ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
____/____/_/ /_/ /_/ .___/____/____/___/_/
                    /_/
Composer 2.2.6 2022-02-04 17:00:38
...

However, upon switching PHP versions, from 8.1 to 7.1, I get an error…

update-alternatives --config php
There are 3 choices for the alternative php (providing /usr/bin/php).

  Selection    Path                  Priority   Status
------------------------------------------------------------
  0            /usr/bin/php.default   100       auto mode
  1            /usr/bin/php.default   100       manual mode
  2            /usr/bin/php7.1        71        manual mode
* 3            /usr/bin/php8.1        81        manual mode

Press <enter> to keep the current choice[*], or type selection number: 
2

php -v
PHP 7.1.33-51+ubuntu22.04.1+deb.sury.org+1 (cli) (built: Jan  6 2023 15:54:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.33-51+ubuntu22.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

composer
PHP Parse error:  syntax error, unexpected '|', expecting variable (T_VARIABLE) in /usr/share/php/Composer/IO/BaseIO.php on line 163

Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE) in /usr/share/php/Composer/IO/BaseIO.php on line 163

help