Demo Code Overview 0:00Alright, let's work on getting Xdebug set up with phpStorm. So here you can see for our demo, I have a very trivial bit of code. So it looks like we have some function called notifyUserOfDriverStatus, and when we trigger it, we pass in the user's name, their age, and then we see if they are of driving age, and where I live, that's 16, and if it is, we set up a message, and then simply return a nicely formatted string. That's all we're doing here. And if you want, we could save that to status. So pretty trivial, right? But that'll work fine for the purposes of this demo. Var-Dump Debugging 0:29So pretty trivial, right? But that'll work fine for the purposes of this demo. Now how would you go about checking to see if this works? And there's probably a good chance that you would just do something like this, vardumpStatus, and then you would view this in the browser. So if I boot up a server and check that out, sure enough we get John Doe, you may drive. And that's how you do your development. We often call this vardump-driven development. And I'm not going to beat you up for it. I would do it too.And I'm not going to beat you up for it. I would do it too. This is a setup I use quite a bit, even DD, which comes as part of Laravel. So these are really useful. But admittedly, they're just kind of quick fixes when you want to spit out a value. If you're doing anything a little bit larger, it makes good sense to use a dedicated debugger like xdebug. So let's get that set up. Now if I go up to run, down to debug, let's debug this single PHP script. And you'll see here at the bottom, PHP interpreter is not installed. Configure PHP Interpreter 1:14Now if I go up to run, down to debug, let's debug this single PHP script. And you'll see here at the bottom, PHP interpreter is not installed. So that means phpStorm doesn't know where our installation of PHP is. So you could click on fix, but let's do it manually so you know where to find it. Right down here in preferences under PHP, we need to provide an interpreter. So let's click browse. And here you can see on my recording machine, I just used a nice installation of MAMP for the various demos. Nice and easy to use. In that case, all I need to do is specify the parent bin directory for where my PHPNice and easy to use. In that case, all I need to do is specify the parent bin directory for where my PHP executable is loaded. It's very possible that in your case, it might be something like user local bin. Something like that. But if I return mine, also notice that right below here, it picks up on the fact that I have xdebug installed. However, it's possible that if you are working along, it'll say something like no debugger or not installed. And that likely means you don't have xdebug set up on your machine or you don't have it Install and Configure Xdebug 2:13or not installed. And that likely means you don't have xdebug set up on your machine or you don't have it configured. So that's our next step. We need to download it. So you could go to xdebug.org, click on download, and especially if you are a Windows user, you can find the correct binary for whatever you need here. On Mac, you could download the source and compile it. However, that can be a little tricky. Instead, I would recommend using something like Homebrew, which I would say is a requiredHowever, that can be a little tricky. Instead, I would recommend using something like Homebrew, which I would say is a required install for every Mac developer. That way, you could do something like this. Brew search, xdebug, and then you could download the appropriate version for whatever you need. So if you are on PHP 5.5, brew install php5.5-xdebug, and that'll pull it in. Next, you need to configure it properly. So if I run php and then pass the ini flag, this is really important. What I've found is a lot of people will add the necessary configuration to their configuration file, and then they find it doesn't work.What I've found is a lot of people will add the necessary configuration to their configuration file, and then they find it doesn't work. And usually what ends up happening here is there's either more than one configuration file and they edited the wrong one, or the version of PHP that they think they are using actually isn't the one that's being used. So they edit one file, however, that has no effect on anything once they run their project. So that's why it's important to see, all right, the loaded configuration file is right here. So all you would want to do is open this file up, and at the very bottom, you want to add your xdebug configuration. So if you want, you can pause the video and take a look at this, but mostly I'm settingyour xdebug configuration. So if you want, you can pause the video and take a look at this, but mostly I'm setting my zend extension equal to that xdebug.so file that you could have just pulled in through Homebrew. Alternatively, if you are using something like MAMP, then it's very possible that xdebug will be included by default. Just hunt around your machine to find where the path is. Or this might be set up for you automatically. It just depends. Next, just make sure you set your host to the default.It just depends. Next, just make sure you set your host to the default. The port is 9000, and once again, by default, PHPStorm will try to connect on that port. So once you've finished that, save your file, restart everything. When you come back to PHPStorm and you reference your PHP home, if you did everything correctly, you should see xdebug listed here. However, if you're not, another way to check to see if this works is to close this out, and you know what, at the top, let's just run a quick PHP info here, comment all of this out, and now if I run this in the browser, here we are, and if I scroll down, if it is correctly installed, you should see xdebug listed within this copyright section right Set Breakpoints and Step 4:38this out, and now if I run this in the browser, here we are, and if I scroll down, if it is correctly installed, you should see xdebug listed within this copyright section right here. So if you see that, you know it's installed, just give PHPStorm a restart, and I think it should work for you. Now when you return, you should be able to set the interpreter as you would expect. So let's uncomment this and remove that line, and we are ready to move forward. So now, once again, let's try to run debug, but notice that we're just not getting any feedback, and that's because we ran the debugger, however, we didn't specify any breakpoint. We can do that simply by clicking on any line here in the sidebar.feedback, and that's because we ran the debugger, however, we didn't specify any breakpoint. We can do that simply by clicking on any line here in the sidebar. So in our case, why don't we click right here. Let's set a stop point or a breakpoint right there. Now once again, I can go back here, or notice that I can press Ctrl D, so I will do that. So here in the middle section, you'll see that it will spit out every registered variable. In our case, as of yet, we don't have anything, so all we see is the environment and server superglobals. Now within here, you'll see all of these confusing icons, and this confused me a lot when I was first learning, but luckily, it's really easy.Now within here, you'll see all of these confusing icons, and this confused me a lot when I was first learning, but luckily, it's really easy. So the main ones you need to know is this one right here, stepover, as well as stepinto. Now the easiest way to learn this is to see it in action. So if I click stepover, that means we've stopped the execution at this point, and if we want to stepover, let's try it, we see disconnected, meaning the script has finished executing. But if I start it up again, this time by clicking on the bug, now I'm going to stepinto, and notice that it goes into the called function here. So think of stepover as saying, I don't really care about the internals of this function here, I want to stepover that and continue on.So think of stepover as saying, I don't really care about the internals of this function here, I want to stepover that and continue on. That means within this line here, the exact same will be true. We could stepinto this function, or we could stepover it. Let's try both. Stepover will simply ignore the internals of this function, and now at this point, we've stopped right before we return. Also notice within the variables, we can see what each one is assigned to. So you can refer here, name is John Doe, message is You May Drive, or you can hover over any of these variables here, and wait just a second.So you can refer here, name is John Doe, message is You May Drive, or you can hover over any of these variables here, and wait just a second. So if we want to see what name is currently assigned to, John Doe, and age is 17. Now let's stop it and restart it again to try the other option. So stepinto, and this time I will stepinto this one as well. And now we're inside of this function. Now if we want to take a look, it looks like age is currently set to 17, and also I could even hover over right here to see the output of that expression, and in this case that's true. So now at this point, stepover, stepinto, and stepout will effectively do the exacttrue. So now at this point, stepover, stepinto, and stepout will effectively do the exact same thing. There's nothing to stepinto, so if I click that, it'll just return us. So that would be the same thing as if I stepped out of that function. And just to make sure that's clear, let's try it one more time, again just because it confused me at first. I want to make sure that we're all on the same page. All right, so now we're within here. We could stepout, and even if there were additional lines, we would stepout of that function andAll right, so now we're within here. We could stepout, and even if there were additional lines, we would stepout of that function and return here. Finally, once you're done debugging, if you just want to finish execution, you can press the play button right here, and that'll finish up. Now as you might imagine, there will be a lot of variables right here. In our case, we just have some simple options here, so there's not too many. However, in a real application, there could be a lot. So that's where the watches tab comes in. Maybe you really want to keep an eye on what some particular variable is doing. Watches and Conditional Breaks 8:18So that's where the watches tab comes in. Maybe you really want to keep an eye on what some particular variable is doing. Maybe there's some kind of bug that you can't explain, where name ends up turning into something that you wouldn't have expected. So what you could do is say, I want to keep an eye on this name variable. That way, as you debug, you can keep an eye on this to see if it changes. To better demonstrate that, why don't we stop this and initialize message to blah. It doesn't matter, just to show that it's being overwritten. Now I will start up my debugger again, and I want to keep an eye on message. So right now, notice that it says, cannot evaluate expression, and that's because itNow I will start up my debugger again, and I want to keep an eye on message. So right now, notice that it says, cannot evaluate expression, and that's because it hasn't been declared or initialized yet. Let's go ahead and step in. But now even here on this line, you can see it still hasn't been evaluated, and that's important to know about your breakpoints. Right now, we've stopped the execution right here, but we haven't executed that code. So let's step over. And now notice that message has been initialized to blah. And once again, if you need to, you can just hover over the variable directly.And now notice that message has been initialized to blah. And once again, if you need to, you can just hover over the variable directly. So this time, I'm not interested in stepping into the contents of that, so I will step over it. Now notice that it picked up on that change, and message is now equal to you may drive. And once again, just to drill this in, you can hover over the variable at any time. So isOfDrivingAge is being past 17, at which point message is being initialized to you may drive. Now there's even other things we could do. For example, let's remove our breakpoint, and let's say we want to break right here.Now there's even other things we could do. For example, let's remove our breakpoint, and let's say we want to break right here. However, we only care about breaking if, for example, there's some kind of condition that passes. Well, I can right-click and set my condition here. And now the condition will be that age is equal to, how about, 15. Alright, so now this will only be active if what we pass is equal to 15. That means if we run the debugger, it'll fully process and execute without ever stopping. But now let's change it. 15, run it, and now that condition will pass, at which point we do break.But now let's change it. 15, run it, and now that condition will pass, at which point we do break. So this can be really helpful in a number of cases. Alright, so I will hit Command-W, and that will do it for your 101 introduction to XDebug. And follow-up lessons will dig further, specifically within the context of a Laravel application.