Skip to content

Instantly share code, notes, and snippets.

@matheusfaustino
Last active April 29, 2020 19:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matheusfaustino/66d0adaa36ed50535606c483dbd69869 to your computer and use it in GitHub Desktop.
Save matheusfaustino/66d0adaa36ed50535606c483dbd69869 to your computer and use it in GitHub Desktop.
Zsh function to change php version per project (directory) using phpbrew
# Add at the end of your .zshrc file
# After every command `cd`, it will check a ".phpbrew" file in the current directory and if it exists it will change the version of php automatically
# Create a .phpbrew file like this: `echo "7.4.4" > .phpbrew` in the folder you want to use the specific version of php
function phpbrew_change_by_dir() {
emulate -L zsh
if [ -f ".phpbrew" ]; then
PHPBREW_LOCAL_VERSION=$(cat .phpbrew);
COMMAND="phpbrew use ";
if [ $(php -r "echo phpversion();") != $PHPBREW_LOCAL_VERSION ]; then
eval "$COMMAND $PHPBREW_LOCAL_VERSION";
fi
fi
}
# hook function
chpwd_functions=(${chpwd_functions[@]} "phpbrew_change_by_dir")
# run command when open a new shell session
phpbrew_change_by_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment