Symfony 4.0搭配MAMP PHP7.2时bin/console文件PHP语法错误求助
Ah, I’ve seen this exact headache with MAMP users so many times—let’s break this down clearly.
The syntax error is triggered by the null coalescing operator (??) in that line: $_SERVER['APP_ENV'] ?? 'dev'. This operator was introduced in PHP 7.0, so any PHP version older than that will throw a syntax error when it hits this code.
Here’s the catch: even though you’ve set MAMP to use PHP 7.2 for your web environment, your terminal (command-line interface, or CLI) is almost certainly using an older PHP version. Most macOS systems come pre-installed with PHP 5.x, and if you haven’t configured your environment variables to point to MAMP’s PHP, running php bin/console will default to that outdated system PHP instead of your MAMP 7.2 setup.
How to Fix This Step-by-Step
Check your CLI PHP version first
Run this command in your terminal to confirm:php -vIf the output shows a version lower than 7.0, that’s definitely the problem.
Point your terminal to MAMP’s PHP 7.2
Open your shell configuration file (this is either~/.bash_profile,~/.bashrc, or~/.zshrcdepending on which terminal shell you use) and add this line at the end:export PATH=/Applications/MAMP/bin/php/php7.2.XX/bin:$PATHReplace
php7.2.XXwith the exact folder name of your PHP 7.2 installation in MAMP (you can find this by navigating to/Applications/MAMP/bin/php/—it’ll look likephp7.2.34or similar).Reload your shell settings
Run this command to apply the changes right away:source ~/.bash_profile # Use ~/.zshrc if you're using ZshNow re-run
php -v—it should show PHP 7.2 as the active version.Test the Symfony console again
Runphp bin/consoleand the syntax error should be gone for good.
Just to confirm: Symfony 4.0 requires PHP 7.1.3 or higher, so your MAMP 7.2 setup is fully compatible—you just needed to make sure your terminal uses the same PHP version as your web server.
内容的提问来源于stack exchange,提问作者Baba Zannah

