← All writing
 ·  1 min read

The M1 macOS Bash PHP Functions I Use

I have several bash functions that come in handy every day when working in PHP on my M1 Mac. Enjoy!

The M1 macOS Bash PHP Functions I Use

I have several PHP bash functions that come in handy every day when working on my M1 Mac. These functions help me in several ways:

  1. php74, php80, php81, php82 and so forth allow me to run any PHP version without unlinking and relinking the working version of PHP.
  2. phpv allows me to change the working version of PHP on-the-fly.
  3. phpd allow me to quickly enter debug mode in Xdebug for any version of PHP.
  4. phpdebug allows me to enter debug mode in Xdebug for the working version of PHP.
  5. phpkill allow me to end all php-fpm processes.
bash
# php by version with homebrew install version
function php72() { /opt/homebrew/opt/php@7.2/bin/php "$@" }
function php73() { /opt/homebrew/opt/php@7.3/bin/php "$@" }
function php74() { /opt/homebrew/opt/php@7.4/bin/php "$@" }
function php80() { /opt/homebrew/opt/php@8.0/bin/php "$@" }
function php81() { /opt/homebrew/opt/php@8.1/bin/php "$@" }
function php82() { /opt/homebrew/opt/php@8.2/bin/php "$@" }

# php switch to homebrew install version
# > phpdebug artisan migrate:up
function phpv() {
    brew unlink php
    brew link --overwrite --force "php@$1"
    php -v
}

# php current version with xdebug options
# > phpdebug artisan migrate:up
function phpdebug() { 
    php -v
    XDEBUG_MODE=debug XDEBUG_SESSION=1 php "$@" 
}

# php version with xdebug options
# > phpd 80 artisan migrate:up
function phpd() {
    if [[ "$1" =~ ^[0-9]+$ ]]; then
        php$1 -v
        XDEBUG_MODE=debug XDEBUG_SESSION=1 php$1 "${@:2}"
    else
        echo "A PHP version number is required: 74, 80, 81, 82"
    fi
}

# kill all php-fpm processes
# > phpkill
function phpkill() { sudo killall php-fpm }

Note, If you are coming from my previous article on running multiple versions of PHP on macOS you may need to unalias the simpler versions of php74, php80, php81, and so on.