Check Xdebug Installation0:00
All right, everybody. I hate to say it, but like all good things, which is a little self-serving, but I really do hope in your mind this qualifies, like all things, I will rephrase this series to shall end. So maybe it's only appropriate that we wrap up with a review of debugging within PHPStorm and what that workflow looks like to connect to Xdebug, which, to be frank, at least for me, I've always found to be a little frustrating to set up in the past. So let's see how we do here. Now, of course, before we can connect to Xdebug, we first have to ensure that it's installed. So let's do this. Why don't you work along with me? Open up your terminal and run php -v. All right, so you can see on my machine, I have php8.2.5, and this is the gold right here. If you have Xdebug installed, you'll see a little note there, and in my case, I have Xdebug 3 installed. So yeah, on your machine, if you also see
and this is the gold right here. If you have Xdebug installed, you'll see a little note there, and in my case, I have Xdebug 3 installed. So yeah, on your machine, if you also see Xdebug 3, you know what? You're all set to go, and you can even fast forward or scrub forward three or four minutes. But yeah, otherwise, if you see Xdebug 2, you're probably fine, but just go through the upgrade process to bump up to version 3. Otherwise, if you don't see any reference to Xdebug, we need to install it, and here's how. Browse to xdebug.org, then visit install, and yeah, like everything, this will depend on your OS. But yeah, if you use Linux or Mac or Windows, just follow the corresponding documentation. So it looks like in my case, I'm on a Mac, and we can install it through Homebrew. So if you don't have Brew installed, or you can do things like this, well, that would be the first step.
PHP Extended Community Library, I think, pretty close. So yeah, just run that and paste it in. Now, I'm guessing it'll fail. Yeah, on my machine, it fails because I've already run through these steps. And I do have the latest version at the time of this recording. Yeah, in your case, it'll take a couple minutes or so. And there might even be a few instructions that you need to follow. All right, so at that point, theoretically, it should work. And I say theoretically, because sometimes you might have to, for example, if you use Laravel Valet, you might want to try running valet restart. If no matter what you do, you can't see Xdebug showing up when you run php -v. Yeah, at that point, just ask a question in the comments below, and the rest of us will do our best to help you. But yeah, hopefully, that should do the trick. The next step is to visit phpStorm. And let's go into Settings,
Configure PHPStorm and php.ini3:04
the comments below, and the rest of us will do our best to help you. But yeah, hopefully, that should do the trick. The next step is to visit phpStorm. And let's go into Settings, go up to PHP. And right here, I want you to ensure that the PHP language level and the CLI interpreter match what you use on the command line. So if I were to say, for example, which php, we want to make sure that those point to the exact same thing. Okay, next, we want to have a look at our php.ini file. And there's a couple ways to do this. First, within phpStorm, I could click here. And it looks like I have a few installed. But yeah, this one at the top is the one I need. And notice, well, if I have Xdebug up and running, I'll see a little note right here. But otherwise, you can open the configuration file directly from phpStorm, like so. And now if I close this out, yeah, this is my php.ini file. Okay,
I'll see a little note right here. But otherwise, you can open the configuration file directly from phpStorm, like so. And now if I close this out, yeah, this is my php.ini file. Okay, so yeah, as part of the Xdebug process, there are a couple things they will want you to add. And you'll see I added them at the bottom below. So yeah, if you want, I will make this available in one of the comments. And you can just paste it in. And then finally, you will want to make sure that these match your local settings. So here's how we do that. Once again, go back into your settings area. And we will go into phpDebug. And actually, the phpStorm team has some nice pre-configuration steps that you can follow. So yeah, if you want to get this working, install Xdebug. And then if you want, you can validate your debugger configuration. And I'll show you about that in just a minute. Then you will install a browser bookmarklet.
Validate Debugger Setup5:19
update or merge it however you need to. Otherwise, append this at the bottom or the top, and that should be fine. Okay, so once again, let's go back into the debug section. And what is this validate debugger configuration on the web server? This is actually pretty useful. So if you want, you can paste the full output of the phpinfo function or into this text area, and it will scan it and let you know if it seems to be in order. So why don't we do that. And there's a couple ways to do this. But if you want, you could just return phpinfo, or you could run it in the command line and then copy that to your clipboard. Alright, so let's go to Laravel.test, give it a refresh. And yeah, I'm just going to copy this whole thing, press Command C. And then I will, here it is, I will paste it in here and validate. Alright, and it's going to let you know if there's if there's anything to worry about. And it seems like down here, this seems fine. Port 9003 is
Connect and Hit Breakpoints6:56
any of these. Or you can do it manually if you want. But this is a simpler solution. So take the browser bookmarklet approach. Alright, once you've done that, we are going to enable listening for debug connections, I can either click here, or I can do it within the run tab or menu, right down here at the bottom, start listening for PHP debug connections. Alright, so yeah, if we did everything correctly, and I think we did, if we add just a little bit of luck, hopefully, it should work. So here's what I'll do. I will set a breakpoint on this line. And I can do that by simply clicking off to the left. Then let's go back to the browser. Let's make sure that our debugger thing is enabled. And then I will refresh. And it instantly kicks me back here. Okay, so now it's possible on your machine, the first time you run it, you'll see a little pop up about an incoming connection. And if so, it may say, set your path mappings, just click on the directory name, press OK. And then you will
Debugging Workflow Demo7:46
machine, the first time you run it, you'll see a little pop up about an incoming connection. And if so, it may say, set your path mappings, just click on the directory name, press OK. And then you will be directed here. Okay, so now I can see all information about the current request, the what's in the cookie, what's in the environment, what the server variables are, what the what the stack looks like. And at this point, I could either continue the request or resume the program. Or I could evaluate or inspect the current environment. So let's do this. Let's click play to continue all the way. And let's make a slight tweak here. So let's say we accept some kind of name from the request, something like this. And then we will spit out the typical hello world. But maybe we will tweak it where we have various greetings. So I could create an array called greetings, one would be hello, one would be what is up, and a third one could be hey there. And yeah,
will tweak it where we have various greetings. So I could create an array called greetings, one would be hello, one would be what is up, and a third one could be hey there. And yeah, I just want to grab a random greeting from the array, and then concatenate that with the user's name, just for a simple example. So I could do something like Laravel's array, random greetings, and then a space, actually a comma, and then a space, and then finally, the name. All right, so yeah, if we were to give this a run in the browser, let's set name to Frank. And sure enough, we get what is up Frank. If we do it a few more times, it will cycle between those various greetings. Okay, so now clearly, that's not the lesson here. The lesson is how to debug this if we need to. So why don't we set a breakpoint right here, come back, give it a refresh. And yeah, now I can see again, the environment as it exists at this point. This breakpoint, you can literally
a little inlay hint here where $name was set to Frank. But yeah, if I want to test how it will behave if I set the $name to a different string, at this point, I can continue, and we should see some kind of greeting and then my own name in the browser. And sure enough, we do. So yeah, in your Laravel apps or really any app, of course, you now have a third tool for debugging at your disposal. So maybe you use dd for simple things. Maybe you've installed Laravel Ray, which is great as well. But now you have a third option, which is Xdebug, and it's incredibly, incredibly powerful once you get it up and running. So give it a shot.
