W3docs

Use different PHP version CLI executable for one command

You can use the alias command in your terminal to create a new command that runs a different version of PHP.

You can use the alias command in your terminal to create a new command that runs a different version of PHP. For example, if you have PHP version 7.4 installed and want to create a new command that runs PHP version 8.0, you can use the following command:

Example of creating a new command that runs PHP version 8.0

alias php80='/usr/bin/php8.0'

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

This creates an alias called php80 that runs the PHP 8.0 executable located at /usr/bin/php8.0. To run a command using this PHP version, you can prefix the command with php80. For example:

Example of running a command using PHP 8.0

php80 -v

This will display the PHP 8.0 version information.

Please note that the path to the PHP executable may vary by operating system. The example uses /usr/bin/php8.0, which is common on Debian/Ubuntu. On macOS, Homebrew typically installs it to /opt/homebrew/bin/php8.0 (Apple Silicon) or /usr/local/bin/php8.0 (Intel). You can locate the exact path by running which php8.0 or find /usr/bin -name 'php*'.

By default, aliases only last for the current terminal session. To make them permanent, add the alias command to your shell configuration file (e.g., ~/.bashrc or ~/.zshrc). The above command assumes that PHP 8.0 is already installed on your system; this step only creates the alias.