Quick Answer

Use Homebrew to install PHP on macOS.

Understanding the Issue

Homebrew provides an easy way to install and manage multiple PHP versions on macOS.

The Problem

This code demonstrates the issue:

Bash Error
# Need PHP on macOS

The Solution

Here's the corrected code:

Bash Fixed
# Install Homebrew (if needed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install PHP
brew install php

# Install specific version (e.g., PHP 8.1)
brew install php@8.1

# Link installed version
brew link --overwrite --force php@8.1

# Add to PATH
echo 'export PATH="/opt/homebrew/opt/php@8.1/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

# Verify
php -v

Key Takeaways

Homebrew simplifies PHP installation and version management on macOS.