php ๐Ÿš€
xdebug ๐Ÿš€
PHP Xdebug Visual Code or PHP Storm

UPDATE: Apr 18, 2023

Ok so Xdebug: Documentation ยป Installation are good directions I just suck at paying attention to them.

1) This area is key Xdebug: Documentation ยป Installation

2) I still need to make an edit to the php.ini

zend_extension=xdebug

What is says above is do not do

extension=xdebug.so

Lastly the install command

pecl install xdebug

Seems to do the job

UPDATE: PHP 8 and Xdebug 3 2021.07.18

Ok this took a bit since the Xdebug site talks about arch -x86_64 sudo pecl install xdebug which did not work.

I just did the normal

sudo pecl install xdebug

```

Then I edit the addition it made:

/opt/homebrew/etc/php/8.0/php.ini
zend_extension="xdebug.so"

xdebug.mode = debug

xdebug.startwithrequest = yes

I moved it all to the bottom of the file too. The docs talk about OPCache but not sure this really helped.

Anyways after this and a PHP restart (see below) it all worked.

I ran valet restart since I am using that for PHP.

Then ran php -v to see

PHP 8.0.8 (cli) (built: Jul 12 2021 02:58:53) ( NTS )

Copyright (c) The PHP Group

Zend Engine v4.0.8, Copyright (c) Zend Technologies

    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans

    with Zend OPcache v8.0.8, Copyright (c), by Zend Technologies

And my launch.json for VSCode is:

{

    // Use IntelliSense to learn about possible attributes.

    // Hover to view descriptions of existing attributes.

    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387

    "version": "0.2.0",

    "configurations": [

        {

            "name": "Listen for Xdebug",

            "type": "php",

            "request": "launch",

            "port": 9003

        },

        {

            "name": "Launch currently open script",

            "type": "php",

            "request": "launch",

            "program": "${file}",

            "cwd": "${fileDirname}",

            "port": 0,

            "runtimeArgs": [

                "-dxdebug.startwithrequest=yes"

            ],

            "env": {

                "XDEBUG_MODE": "debug,develop",

                "XDEBUGCONFIG": "clientport=${port}"

            }

        },

        {

            "name": "Launch Built-in web server",

            "type": "php",

            "request": "launch",

            "runtimeArgs": [

                "-dxdebug.mode=debug",

                "-dxdebug.startwithrequest=yes",

                "-S",

                "localhost:0"

            ],

            "program": "",

            "cwd": "${workspaceRoot}",

            "port": 9003,

            "serverReadyAction": {

                "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",

                "uriFormat": "http://localhost:%s",

                "action": "openExternally"

            }

        }

    ]

}

> UPDATE: PHP 7.4

Every time I do value use php I then lose my xdebug setup :( here is me taking a moment to stop having that happen

```

vim /usr/local/etc/php/7.4/php.ini

```

Then add this

[PHP]

zend_extension="xdebug.so"

xdebug.remote_enable = 1

xdebug.remote_autostart = 1

xdebug.remote_handler = dbgp

xdebug.remote_host = 127.0.0.1

xdebug.remote_port = 9000

xdebug.remote_mode = req

And then I am set again! Notice I am using 9000 above my insanity of using 9005 below has passed

I put two files in my home directory, one starts with the above and one does not. Then I put this alias into my ~/.zshrc

alias debug_on="cp ~/Code/php.ini.debugon /usr/local/etc/php/7.4/php.ini"

alias debug_off="cp ~/Code/php.ini.debugoff /usr/local/etc/php/7.4/php.ini"

source ~/.zshrc

and now I can easily turn this on and off to save TONS of time with any php job (testing, etc)

END UPDATE

This article got me going on using Xdebug [https://tighten.co/blog/configure-vscode-to-debug-phpunit-tests-with-xdebug](https://tighten.co/blog/configure-vscode-to-debug-phpunit-tests-with-xdebug)

But since 7.2 it has been tricky on the Mac.

pecl install xdebug

Then vim /usr/local/etc/php/7.2/php.ini

And remove from there any reference to xdebug.so

Then /usr/local/etc/php/7.2/conf.d/ext-xdebug.ini

[xdebug]

zend_extension="xdebug.so"

xdebug.remote_autostart=1

xdebug.remote_port=9005

xdebug.remote_enable=1

xdebug.profiler_enable=1

xdebug.profileroutputdir="/Users/alfrednutile/xdebugtmp/"

and restart the services

brew services restart php72 

You should be able to run php --ini at the command line.

Configuration File (php.ini) Path: /usr/local/etc/php/7.2

Loaded Configuration File:         /usr/local/etc/php/7.2/php.ini

Scan for additional .ini files in: /usr/local/etc/php/7.2/conf.d

Additional .ini files parsed:      /usr/local/etc/php/7.2/conf.d/ext-opcache.ini,

/usr/local/etc/php/7.2/conf.d/ext-xdebug.ini,

/usr/local/etc/php/7.2/conf.d/php-memory-limits.ini

Visual Code's xdebug launch.json looks like:

{

  "version": "0.2.0",

  "configurations": [{

      "name": "Listen for XDebug",

      "type": "php",

      "request": "launch",

      "port": 9005

    },

    {

      "name": "Launch currently open script",

      "type": "php",

      "request": "launch",

      "program": "${file}",

      "cwd": "${fileDirname}",

      "port": 9005

    }

  ]

}