Running PHP Locally on macOS: A Developer's Sandbox
If you are used to the Node.js workflow, the mental model is straightforward. You drop a script named test.js containing console.log('hello world'), execute it via the CLI with node test.js, and immediately see output in your terminal. It requires no web server configuration, no virtual host setup, and absolutely no Apache installation.
Porting that specific mental model to PHP on macOS often hits a wall for beginners. The user wants to run test.php containing echo('hello world'); simply by typing php test.php. While the syntax looks identical to the Node.js equivalent, the underlying execution context differs significantly between the two ecosystems on modern macOS systems.
The command php test.php works perfectly fine if a PHP interpreter is installed and accessible in your system path. However, there is a distinct difference between having an interpreter for scripting (CLI) versus having a server environment (Apache/Nginx). The user's constraintâavoiding the installation of Apacheâis actually the default state for most modern development workflows. You do not need a web server to execute PHP code from the command line; you only need the CLI binary.
To achieve this on macOS, the first step is ensuring the php executable exists in your shell environment. On older versions of macOS (Catalina and earlier), PHP might have been part of the base OS installation, but starting with macOS 10.15, Apple deprecated the system PHP support. Consequently, the default command often points to a broken or outdated version, or not at all.
The recommended approach for a robust developer sandbox is using Homebrew. Running brew install php installs the latest stable version of the PHP interpreter along with its CLI tools. Once installed, you can verify the setup by running:
php -v
This command confirms the version and ensures the binary is reachable from your current directory. If this returns version information without error, you can proceed to run your script exactly as requested:
echo 'hello world' > test.php
php test.php
This will output hello world directly to your terminal. This setup satisfies the requirement for a sandbox that mimics the Node.js experience without the overhead of a web server stack.
However, there are technical nuances to consider regarding environment isolation and configuration. When running php test.php, the script executes in CLI mode. This means it does not automatically have access to the $_SERVER variables populated by Apache or Nginx, nor does it load any .htaccess files. If your code relies on web-specific functions like $_GET or session handling without explicit initialization, those will fail or behave unexpectedly in this context.
Furthermore, while Homebrew is excellent for a quick sandbox, managing PHP dependencies can become tricky if you are using Composer. The Homebrew formula often installs its own vendor directory at /usr/local/var/php/..., which might not align with the global composer install expectations if you have other PHP versions installed (like via phpenv or via macOS tools). For a simple "hello world" script, this is negligible. But if your sandbox needs to load complex libraries, you might need to ensure Composer knows where to look for extensions and autoloaders relative to the specific Homebrew installation path.
Another consideration is the PHP version compatibility with your codebase. If you are developing code intended for a specific production stack (e.g., Laravel 10+ requiring PHP 8.1 or higher), ensuring your Homebrew PHP matches that requirement is critical before writing any business logic. You can check this via php --version and compare it against your project's requirements file if one exists.
Finally, while a full web server like Apache provides routing capabilities and CGI execution environments, the CLI mode is strictly for script execution. If you eventually need to test HTTP requests or server responses, you will still need to spin up a lightweight server (like PHP's built-in php -S or Node.js) regardless of how you installed the interpreter. But for pure logic testing where input comes from the file itself and output goes to the console, the CLI binary is the correct tool.
