After beginning to use AirTables with PHP Curl I decided today to finally learn how to use xdebug for PHP. I’ve known for a while that my echos and vardumps were extremely inefficient compared to using a debugger… so time to sort it out!
A few things to note:
- There are often two versions of Apache stored on MacOS. I have to use sudo /usr/sbin/apachectl restart for the desired effect rather than just apachectl
- XDebug does not support PHP 5, so make sure you are upgraded to 7 before attempting otherwise you’ll waste a bit of time like I did.
- Useful commands
- php –ini
- I went around in circles I actually had three versions of PHP on my system, but I finally ended up with 7.4 as my default.
pecl install xdebug fails
$ which pecl /usr/local/bin/pecl $ pecl install xdebug Build process completed successfully Installing '/usr/local/Cellar/php/7.2.11/pecl/20170718/xdebug.so' Warning: mkdir(): File exists in System.php on line 294 Warning: mkdir(): File exists in /usr/local/Cellar/php/7.2.11/share/php/pear/System.php on line 294 ERROR: failed to mkdir /usr/local/Cellar/php/7.2.11/pecl/20170718
To fix this error just do
rm /usr/local/Cellar/php/7.2.11/pecl This sorts out the symlinks shenanigans, read the above blog if you want more information.
Opening PHP Config Files in VSCode
Rather than trying to use the Finder to open the configuration directory, add VSCode to the command line by ‘command shift p’ and then select Shell Command : Install code in PATH.
Then you can do this to open the folder in VSCode for easy editting.
$ /usr/local/etc/php/7.2 $ code .
Basically remove the xdebug reference at the top of your php.ini since it points to the wrong place anyway.
Then, something I never knew, php will read through any ini files you put in the conf.d directory… so we put all the setup in there.
Add your XDebug Config
;XDebug zend_extension="/usr/local/Cellar/php/7.2.11/pecl/20170718/xdebug.so" xdebug.remote_autostart=1 xdebug.remote_port=9000 xdebug.remote_enable=1 xdebug.profiler_enable=1 xdebug.profiler_output_dir="/Users/simon/xdebugtmp/"
(Change appropriate to your extension pathway, dependant on version… and also the profiler output)
(Switch profiler off when you dont need it)
php –ini should now show the xdebug bits correctly.
Follow PHP Debug instructions, but basically:
Install the extension & reload
Hit gear icon in the debugging and choose php
This creates a file in .vscode/launch.json where your config is.
It didn’t work ! 🙁
So, after a bit of fiddling I realised that this laptop I am working on is still stuck in the slow lane with PHP 5.6
My php -v stated 7.2.11 but my phpinfo on my local apache was still on version 5.
I decided to upgrade to PHP 7.4 to get the latest version.
Albeit realising that I needed to upgrade my OSX !
On upgrade success you will be told how to adjust apache configuration to point to the newer version of PHP, and not (afaik) the version (5) that got shipped with older versions of MacOS.
$ brew update $ brew upgrade php (Initially the upgrade failed because I had to upgrade the command line tools package through Software Update on MacOS) $ code /etc/apache2/httpd.conf
Comment out current version of PHP, and add
LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
And also check DirectoryIndex looks like:
<IfModule dir_module> DirectoryIndex index.html index.php </IfModule>
Then some restarting
brew services start php sudo apachectl restart
httpd: apr_sockaddr_info_get() failed Could not reliably determine the server’s fully qualified domain name
It turns out that many of us end up having two variants of Apache on MacOS. I have no idea why, and all I needed to actually do was use
sudo /usr/sbin/apachectl restart
And all of a sudden my phpinfo page was finally showing PHP 7.4.5 … now I could go back to the original point of trying out XDebug!
However, phpinfo() was not saying anything about XDebug which means it didnt see it being installed. This could have been the fact that I upgraded PHP after installing via PECL and so the configuration got overwritten.
So many hoops to jump through.
A quick look realised that my previous installation was of PHP 7.2, but my newer install was 7.4 so I just had to copy over the conf/xdebug.ini to the 7.4 folder and should be good to go!
After this phpinfo() showed the configuration file loading, but using php –ini I was having an image not found on the xdebug.so
I saw that actually for some reason my previous pecl installation of XDebug had disappeared … the old directory of 7.2 was gone!
I decided to try pecl uninstall xdebug and then pecl install xdebug and this seemed to do what I wanted. So now I could update xdebug.ini with the newer 7.4
And finally I had xdebug stuff in phpinfo() !
Using XDebug in VSCode
In debug tab, the little cog being pressed will create two options in the dropdown to its left, Listen for XDebug will listen to whatever you’ve got going on, on your local server, and the other is for specific files.
One of the things I love about XDebug straight away is navigating JSON objects … if I’ve done a json_decode I can actually navigate the object far easier than if using var_dump. Massive increase in productivity! Mission Accomplished!
Note: This blog post will be saved for all my PHP installation, configuration, and extension bits so will change over time.