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.
# php by version with homebrew install version
function php72() { /opt/homebrew/opt/[email protected]/bin/php "$@" }
function php73() { /opt/homebrew/opt/[email protected]/bin/php "$@" }
function php74() { /opt/homebrew/opt/[email protected]/bin/php "$@" }
function php80() { /opt/homebrew/opt/[email protected]/bin/php "$@" }
function php81() { /opt/homebrew/opt/[email protected]/bin/php "$@" }
function php82() { /opt/homebrew/opt/[email protected]/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.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join Dare To Code Email List

Get emails from me on full-stack PHP development by subscribing to the Dare To Code mailing list.