PHP Debugging with Visual Studio Code on MacOS

M
2 min readJul 28, 2020

--

Debugging code is one of an essential thing in software developing process. Let us talk about setting up PHP debugging with Visual studio code on MacOS.

Prerequisite

Install following items

  1. MAMP (https://www.mamp.info/en/mamp/mac/)
  2. Terminal
  3. Text editor: Visual Studio Code (https://code.visualstudio.com/)
  4. Visual Studio Code’s Extension named PHP debug (https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug)
  5. Make sure you need to add this to ~/.zshrc
PHP_VERSION=`command ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATH

First thing first, check version of php. Go to MAMP > Preferences > PHP

MAMP preference screen.

Here we go, now , we are using PHP version 7.4.2. The MAMP also provides XDebug so that you need not to install it manually.

Then add following snippets at the bottom line in php.ini

[xdebug]zend_extension="/Applications/MAMP/bin/php/php7.4.2/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so"xdebug.remote_enable=1xdebug.remote_host=127.0.0.1xdebug.remote_connect_back=1    # Not safe for production serversxdebug.remote_port=9000xdebug.remote_handler=dbgpxdebug.remote_mode=reqxdebug.remote_autostart=true

Add the snippets to php.ini for two files.

/Applications/mamp/bin/php/php7.4.2/conf/php.ini
/Applications/mamp/conf/php7.4.2/php.ini

Oh wait, recheck again using phpinfo();

<?php namespace App\Controllers;class Home extends BaseController {   public function index() {
phpinfo();
}
}
// CodeIgniter4 : app/Controllers/Home.php

It must show something like this.

localhost:8888/home/index.php

Now let us start debugging. Go to Run > Start Debugging (Press F5 for Shortcut)

Debugging screen.
Break point

php.ini example snippet (https://gist.github.com/khunemz/758db6c426b6c360facdce71d3fc2826)

Edited by : Chutipong Roobklom

--

--