As much as I enjoy working with Laravel Valet and using Homebrew to manage different versions of PHP, there are still times in which my system has been set up in such a way that my shell is using one version of PHP and Laravel is using a different version.
At the time of this post, I see the following whenever which I run $ which php
:
/opt/homebrew/opt/php@7.4/bin/php
And when I run $ php -v
, I’ll see the following output:
PHP 7.4.33 (cli) (built: Jul 13 2023 17:41:11) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
But if I was to run phpinfo();
within a page hosted via Laravel, I’d get PHP8. This is problematic because if I’m working on a function that I’m eventually going to incorporate in an app that runs in the browser, there may be functionality that doesn’t work as I expect.
As such, it’s important to make sure we have parity between Laravel and our system’s binary. To do this, issue the following command in the terminal:
$ valet use php@7.4
$ valet restart
Obviously, you’ll change the value of php@7.4
to whatever you want to use on your machine. Whatever it is, though, make sure there’s parity between the two. This way, whenever you run a script independently, then you’ll have the same results in the browser as you do in your terminal.