Setting up a PHP environment on Mac involves two main steps: installing PHP and configuring PHP environment variables. By following these steps, you'll be able to easily set up a PHP development environment on your Mac.
Homebrew is a package manager for macOS that helps us quickly install PHP. First, you need to run the following command in your terminal to install Homebrew:
<span class="fun">/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"</span>
Once Homebrew is installed, you can install PHP by running the following command:
<span class="fun">brew install php</span>
After the installation is complete, check if PHP is successfully installed by running:
<span class="fun">php -v</span>
Composer is a dependency management tool for PHP, often used to install frameworks and libraries. You can install Composer via Homebrew by running this command:
<span class="fun">brew install composer</span>
Next, you need to configure PHP environment variables. Open the .bash_profile file by running this command in the terminal:
<span class="fun">vim ~/.bash_profile</span>
At the end of the file, add the following lines to include PHP's binary path in your system's environment variables:
# PHP
export PATH="$(brew --prefix php)/bin:$PATH"
After editing, save and exit the vim editor. To apply the changes, run the following command:
<span class="fun">source ~/.bash_profile</span>
Finally, you can test if PHP environment variables are correctly configured by running:
<span class="fun">php -v</span>
If you see output similar to this, then the PHP environment variables are set up correctly:
<span class="fun">PHP 7.4.21 (cli) (built: Jul 1 2021 19:45:43) ( NTS )</span>
With that, you've successfully set up PHP and configured PHP environment variables on your Mac. You can continue to explore more PHP development techniques to enhance your skills further.