Join Kevin Skoglund for an in-depth discussion in this video Debug and troubleshoot, part of PHP Essential Training.
- [Narrator] In the last movie, we looked at the warnings and errors that PHP generates. But often we need to debug our code, even when we aren't getting a PHP error. We just aren't getting the results that we expected from our code. Maybe a condition that uses less than ought to be using less than or equal to. Or maybe we put code outside of a loop that ought to be inside the loop. As with errors, the more information we have about a problem, the easier it'll be to solve it. And the best way for us to get more information is to output that information to the HTML page as our code is running. Now sure, this extra output is going to make the page ugly while we're debugging, but that's okay because we're not planning to keep it.
It's just temporary. There are a number of ways we can output useful information, and we've seen three of them already. The first one is super simple, we just echo the value of a variable. If you're wondering what the value of x is each time it goes through a loop, just right in the middle of that loop echo out the value of x. We'll see it on the page and we can watch the value change with each iteration of the loop. Echo is also going to be super helpful when we start working with databases, because we're going to assemble SQL statements in a variable and then send that statement off to the database. If we don't get back the results from the database that we expected, we can echo that statement and see what was the SQL statement that we had constructed and we can examine it for errors.
The second one we've already seen is print_r. Remember, that's for printing a readable array. It works with arrays and makes them output in a much more user-friendly way. Remember that this is going to look best if we surround it with HTML pretags that will allow it to preserve the white space and make the formatting even nicer. The third one we've seen before is gettype, and that just returns the type of a variable. Maybe we're working with a string when we ought to be working with an integer. Well, gettype can help us to identify that. There's also some new ones that we should look at, the first of which is var_dump.
And that's going to take everything that it knows about a variable and just dump it out for you. It's going to return the value, it's also going to return the type, and if it's in an array, it's going to give you a nice friendly output of that array. It's essentially doing what all three of the functions up above it did. It's extremely useful, and I think you're going to find that you use it a lot. The next one you should know about is get_defined_vars. And that's going to return an array of all of the defined variables that PHP knows about at that moment. Now that's not just the variables that you've defined, it's going to include a few other special variables that PHP uses.
That's good, it's going to show us everything that PHP knows about variables. Another very helpful one is debug_backtrace, and that's going to show you a backtrace. It's going to return it as an array. A backtrace, also called a stacktrace, is a report of the function calls that have been made that lead up to a certain point in time during the execution of a program. It's like a report of what happened before you got to where you are now. Backtrace, as in, retrace your steps backwards. Let's try some of these. So I'm just going to open up basic.html and we'll do save as, we'll call this debugging.php.
Change the title to debugging, and I'll create my php tags. And I'm just going to assign a couple of variables here. I've got number equal to 99, string equal to Bug?, and array equal to an associative array. So I've got keys and values there. So let's try this var_dump. We won't try the first three because we've already used those before, and let's start with the var_dump. Let's just do var_dump and $number. Let's try that out in a browser. Local host, and for me, that's going to be at kevenskoglund/sandbox/debugging.php.
And there you go. I got back int 99. It's telling me it's an integer and the value is 99. I even got some nice color formatting on that. Let's go try the next one now. We'll just copy this and paste it, and we'll change it from number to string. Let's take a look at what that does. String, here's the value, and it tells me the length of it. Nice little added bonus there. And let's try one more. We'll try it with array. Save it, and there we go. See how it gives me a nicely formatted array? I didn't even have to use pretags around it. It went ahead and just said, here's the array, there are three elements in the array, that's its size, here they are.
One, two, and three are the keys. Here's the values. And it went ahead and further broke down that value and said that value was a string. Here's the value of the string, and here's the length of the string, just like it did up here for this one. So you can see that's super useful, right? Let's take a look at another one. Notice that the var_dump actually did the output. We didn't need to say echo or anything like that. It dumped it to the screen, that's what it does. Now we're going to take a look at get_defined_vars. So let's do php and get_defined_vars.
And let's just do that. We'll reload the page, you'll see we got nothing back. Why is that? Well, it's because what it does is it returns an array, put the br tag in here. So we need to echo that out. But it's an array, so there's no point using echo. We could use print_r and that would work. And remember that print_r looks best when we surround it with pretags. Let's come over here and try that out. And here we go. It's a long list, you see there's lots of these that are things that we didn't set. But if you scroll down here to the bottom, you'll see here's number, string and array. And here's those values for us.
So that can be useful to find out a lot about the variables that PHP knows about. Because that's so long right now, I'm just going to comment that out and then we'll move on down to the next one because I want us to try out this backtrace. Now the debug backtrace also does not output, it just returns an array to us. So let's go ahead, php, but this time instead of using print_r, I'm going to use var_dump debug_backtrace, all one word. So let's just try that and see what that gives us. And here we are. Array size zero, empty.
That's what it gave us. Well, if you think back to what I told you about the debug backtrace, it's a report of the function calls that have been made that lead up to a certain point in time. Well, we haven't made any function calls here, so we need to do that. We need to have a function call. I'm just going to bring up one of the functions that we've worked with before, and that is the say hello to. Say hello to word, remember, this just outputs everyone. Let's just bring that up so you can see it first of all. Hello everyone, that's what it does, all right. So let's say that we're in the middle of debugging this and there's a problem, we can just do a debug backtrace right here.
So let's do var_dump and then debug_backtrace. So we'll do it right after that line and then we'll hit return, you'll see that it comes back and it tells us, here we are, the array size four, and it has the file that we're working in, see, it lists where the file is located, it lists off the line number, so line number 33 is where we are, and the function that we're inside of, we're inside the say hello to function. And then the arguments that have been passed in it also tells us, and that's an array, and everyone was the string that was passed in.
So we get all sorts of useful information from inside that function. And if we have several functions, we'd have functions inside of functions or files that are calling other files, it'll trace its step backwards through all of those and show you all of them. It can be very, very useful. Now, PHP does not have an official internal debugger. Some software programs do have a debugger built into you that can help you debug things, but there are a couple of third party tools that are worth knowing about. The first of these is Xdebug, and Xdebug is probably the most popular debugger for PHP.
It offers things like showing you that stacktrace whenever you have an error automatically. It logs all of the calls to your functions. It will make your var_dump output look even prettier. All sorts of things like that that it'll do for you. It's a really nice companion to have. Another alterative is DBG. You can go to their website and look up more information about that. And then the last one is FirePHP, and this uses firebug for Firefox. So if you're familiar with firebug, it's a useful, popular add-on to the Firefox web browser that gives you more information about what's happening on a webpage.
And FirePHP enables you to log debug information to your firebug console from your PHP application. So that can also be really useful. As I said at the beginning, this kind of debug code is a temporary measure. It's meant to only be there while we solve the problem. Once we're done solving the problem, we want to make sure that we remove that code so that it doesn't accidentally end up in our application permanently.
- What is PHP?
- Embedding PHP code on a page
- Inserting code comments
- Variables, strings, arrays, and Booleans
- If, else, and elseif statements
- While and for loops
- User-defined functions
- Function arguments and return values
- Debugging and troubleshooting