Check what is already installed
Open Terminal and run command -v php followed by php -v if a command is found. The first command shows which executable the shell would use; the second reports its version. A missing command is normal: PHP’s manual says macOS has not bundled PHP since macOS 12. An older system PHP, or a PHP installed for another project, should not automatically be the version used for new work.
Compare the version with the live PHP support table. Also run uname -m to see whether the current terminal reports arm64 or x86_64. On Apple Silicon, a terminal running under translation can report x86_64, so check the Mac’s hardware if the output surprises you.
Terminal — locate PHP if present
command -v phpTerminal — check a found PHP
php -vTerminal — inspect the current architecture
uname -mChoose a maintained installation method
For a supported macOS/Homebrew setup, Homebrew is a practical package manager and is linked from PHP’s official macOS installation guide. Install Homebrew from its official website and follow its current instructions for your Mac. Homebrew’s support differs between Apple Silicon, Intel and macOS releases; check its installation requirements before using it. MacPorts is an alternative if Homebrew does not support your particular system well.
Do not assume that a preinstalled PHP exists or is supported. Installing Homebrew and then PHP is a local development setup; it does not automatically create a public web server. Choose a supported, patched PHP branch compatible with your project and host.
Install PHP with Homebrew
After Homebrew itself is installed and its shell setup has been completed, run brew install php. Homebrew’s php formula tracks its current stable package; its exact version can change. Then check php -v and command -v php. Use brew --prefix to see Homebrew’s actual installation prefix rather than guessing a path.
Apple Silicon and Intel Homebrew installations commonly use different prefixes. If brew or php cannot be found, follow the shellenv line printed by the Homebrew installer and reopen Terminal. Do not copy an Apple Silicon prefix blindly onto an Intel Mac or vice versa. If another PHP appears first in PATH, locate it with command -v php and resolve the order before editing configuration files.
Terminal — install the current Homebrew PHP formula
brew install phpTerminal — identify Homebrew and PHP
brew --prefix
command -v php
php -vCheck PATH and php.ini
PATH is the list of folders your shell searches for commands. Homebrew prints a shell setup step during installation; follow that step for your shell configuration file, usually ~/.zprofile for zsh, instead of inventing a fixed prefix. Open a new Terminal after updating it. If command -v php still selects an older interpreter, inspect your shell configuration for another PHP path.
php --ini shows which configuration file this command-line PHP loads and which additional .ini files it scans. Use that output rather than assuming a path. A web service may use a different PHP process and configuration from the CLI. After installing PHP, php -m lists its loaded extensions.
Terminal — show loaded PHP configuration
php --iniTerminal — list available extensions
php -mRun a first PHP program
Create a project folder, save the example below as hello.php, and execute it with php hello.php from that folder. The output is “Hello, Codectionary!”. PHP, not the shell, reads the .php source. The string contains \n for a newline; echo writes it to the terminal.
If you later want to view a generated webpage, a PHP-capable local web server is needed. Simply opening a .php file in a browser will display or download source rather than execute it. The PHP built-in server is suitable for local learning only; see the request lifecycle guide before using it.
Terminal — create a project folder
mkdir php-first
cd php-firstTerminal — after saving hello.php
php hello.phpCommon macOS setup problems
“command not found: brew” usually means Homebrew is not installed or its post-install shellenv step was not added for this shell. “command not found: php” after installation means the shell is not finding the Homebrew executable; reopen Terminal, inspect command -v php and brew --prefix, and check PATH. An unexpected PHP version often means an older installation precedes Homebrew in PATH.
On Intel Macs, check Homebrew’s current support tier and availability of prebuilt packages; compiling a package may require Apple developer command-line tools. If php --ini points to a different setup than expected, first determine which php executable you are running. An extension can also differ between CLI and web service configurations.
Syntax
<?php
echo "Hello, Codectionary!\n";
Examples
First macOS command-line script
Save as hello.php and run php hello.php in Terminal.
<?php
echo "Hello, Codectionary!\n";
- The <?php tag begins PHP code. The quoted message is a string containing a newline escape.
- echo writes the message to standard output, which Terminal displays as Hello, Codectionary!.
- Edit the message and run php hello.php again; this checks that your editor saved the file in the folder where the shell is working.
Best practices
- Use Homebrew only where its current macOS and hardware support fits your machine; check its official installation guidance.
- Follow the shell setup printed by the installer and inspect command -v php before changing PATH manually.
- Use the supported-versions page and keep the installed PHP package patched.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual