Create a gist now

Instantly share code, notes, and snippets.

@w00fz /
Last active Mar 26, 2017

PHP switcher
# Check if command was ran as root.
if [[ $(id -u) -eq 0 ]]; then
echo "The command \"sphp\" should not be executed as root or via sudo directly."
echo "When a service requires root access, you will be prompted for a password as needed."
exit 1
# Usage
if [ $# -ne 1 ]; then
echo "Usage: sphp [phpversion]"
echo "Versions installed:"
brew list | grep '^php[0-9]\{2,\}$' | grep -o -E '[0-9]+' | while read -r line ; do
echo " - phpversion: $line"
exit 1
currentversion="`php -r \"error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));\"`"
brew list php$newversion 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
echo "PHP version $newversion found"
# Check if new version is already the current version.
# if [ "${newversion}" == "${currentversion}" ]; then
# echo -n "PHP version ${newversion} is already being used. Continue by reloading? (y/n) "
# while true; do
# read -n 1 yn
# case $yn in
# [Yy]* ) echo && break;;
# [Nn]* ) echo && exit 1;;
# esac
# done
# fi
echo "Unlinking old binaries..."
brew unlink php$currentversion 2> /dev/null > /dev/null
echo "Linking new binaries..."
brew link php$newversion
echo "Linking new modphp addon..."
sudo ln -sf `brew list php$newversion | grep libphp` /usr/local/lib/libphp${majorNew}.so
echo /usr/local/lib/libphp${majorNew}.so
echo "Fixing LoadModule..."
apacheConf=`httpd -V | grep -i server_config_file | cut -d '"' -f 2`
sudo sed -i -e "/LoadModule php${majorOld}_module/s/^#*/#/" $apacheConf
if grep "LoadModule php${majorNew}_module .*php${newversion}" $apacheConf > /dev/null
sudo sed -i -e "/LoadModule php${majorNew}_module .*php${newversion}/s/^#//" $apacheConf
sudo sed -i -e "/LoadModule php${majorNew}_module/s/^#//" $apacheConf
echo "Updating version file..."
pgrep -f /usr/sbin/httpd 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
echo "Restarting system Apache..."
sudo pkill -9 -f /usr/sbin/httpd
sudo /usr/sbin/apachectl -k restart > /dev/null 2>&1
pgrep -f /usr/local/"Cellar|opt"/*/httpd 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
echo "Restarting homebrew Apache..."
sudo pkill -9 -f /usr/local/"Cellar|opt"/*/httpd
sudo /usr/local/bin/apachectl -k restart > /dev/null 2>&1
# pgrep -x httpd 2> /dev/null > /dev/null
# if [ $? -eq 0 ]; then
# echo "Restarting non-root homebrew Apache..."
# httpd -k restart > /dev/null 2>&1
# fi
echo "Done."
# Show PHP CLI version for verification.
echo && php -v
echo "PHP version $majorNew.$minorNew was not found."
echo "Try \`brew install php${newversion}\` first."
exit 1

Can you also make this update PHP_INI_SCAN_DIR? Like so:

# Calculate PHP_INI_SCAN_DIR.
export PHP_INI_SCAN_DIR=$(php --ini | grep "Scan for additional .ini files in:" | awk '{print $NF}')

Thanks for doing this! For me, the "non-root homebrew Apache" was running as root so I had to change line 76 (httpd -k restart > /dev/null 2>&1) to be "sudo httpd -k restart > /dev/null 2>&1". Maybe I've got something else wrong that's causing httpd to run as root but FYI to others. I thought it wasn't working properly when looking at phpinfo() refreshes but it was just that apache wasn't restarting.

crecabar commented Nov 6, 2016

The script is a great contribution, as an improvement, it also should update the current PATH, so the php version used on command line match the version used with apache, so the version used with composer or phpunit is the same as the used for the web server.

With this command the path will be updated, the trick it is to change php55 for the version loaded and in .bash_profile (or .bashrc), change it for a variable that match the current version.

export PATH="$(brew --prefix homebrew/php/php55)/bin:$PATH"
w00fz commented Nov 7, 2016

@wimleers I'm not sure what PHP_INI_SCAN_DIR is useful for but I can add it if you tell me where that should go

@crecabar I don't need to do that with homebrew. When the PHP version changes, it is applied globally. I use zsh though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment