Switch php versions on manjaro

I have PHP version 8and i also have PHP 7, but the php -v command only show version 8how can i change environment variable to php 7

my php7 installation directory is usr/bin/php7

What’s the output of:

alias php

if the output is:

bash: alias: php: not found

type:

alias php=/usr/bin/php7

Please test that extensively and if that is indeed what you want, report back and we’ll make that command permanent.

if the output is anything else than:

bash: alias: php: not found

please report back with the output.

I’ve tried alias php but nothing print out in the console. Please help, I need to switch version PHP based on different projects

Hello @ziat,

the ‘alias’ command didnt generate any output. It assign the left term to the right.

It means when you ‘alias php=/usr/bin/php7’ you then can run ‘/usr/bin/php7’ by typing ‘php’ in your console.
This is not permanent, after a reboot this ‘alias’ is gone. If you want this permanent you have to write the ‘alias’ in this file: ~/.bashrc

I would suggest to alias the older to an extra name like php7:

alias php7=/usr/bin/php7

So you can run your scripts with both versions:

php your-script.php

runs your script with actual php version, and

php7 your-script.php

runs your script with php7.

1 Like

Which versions?

if php7 and 8:

  1. With:

    alias php=/usr/bin/php7
    

    executing

    php myPHP 
    

    will run it in 7

  2. With:

    alias php=/usr/bin/php8
    

    executing

    php myPHP 
    

    will run it in 8

@Fabby
I mean when your run ‘php’ without any aliases it runs ‘/usr/bin/php’ which is assigned to the newest version which comes with the rolling releases. At this time version 8.
So I would suggest for older versions an ‘alias’ that he can use both if wanted. :yum:

1 Like

terminal or web project ?
alias is only for terminal and for user (so not useful), for web: change server config (apache…nginx) or (best for me) use docker

if not web project, linux use shebang (shebang “env” not use alias !) :wink:

less /usr/bin/composer
#!/usr/bin/env php
<?php
/*
 * This file is part of Composer.
 *

[patrick@tower ~]$ alias
alias cp='cp -i'
...
alias php='/usr/bin/python'

ok

[patrick@tower ~]$ php -V
Python 3.9.7

but

[patrick@tower ~]$ env php -v
PHP 8.0.11 (cli) (built: Sep 25 2021 07:52:29) ( NTS )
2 Likes