Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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 out put useful information and we've seen three of them already. The first one is super simple, we just echo the value of the 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's 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. 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 its also going to return the type. And if it's an array, it's going to give you a nice friendly output of that array. Essentially doing what all three of the functions up above it do. Its 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 back trace, also called a stack trace is a report of the function calls that 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.
We'll do Save As, and 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 question mark, 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. But let's start with a var_dump, let's just do var_dump and $ number. Let's go try that out in a browser, Local Host and for me, that's going to be @Kevinscoglin/sendbox/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. 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 it one more, I'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 pre tags around it, I went ahead and just said here's the array, there are three elements within 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 is 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 define vars. So let's do, php and get defined vars. And let's just do that and we'll reload and the page and you'll see we got nothing back.
Why is that? Well it's because what it does is it returns an array, the VR tag in here, so we need to echo that out, but it's an array, so there's no point in using echo. We could use print r and that would work, and remember that print r looks best when we surround it with pre tags. Let's come over here and try this out. And here we go. It's a long list. You see that 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 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 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 work remember this just outputs everyone. Lets just bring that out so we can see it first of all. Hello everyone that's what it does. So lets we're in the middle of debugging this and there's a problem we can just do a debug back trace here. So lets do var_dump and then debug. Backtrace. So we'll do it right after that line and then we'll hit Return, and you'll see that it comes back and it tells us, here we are, the array, size 4, and it has the file that we're working in.
See it lists where the file is located, it lists of the line number, so line number 33 is where we are, and the function that we're inside of. We're inside of 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 have functions inside of functions or files that are calling other files it will 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 in to 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 stack trace whenever you have an error, automatically. It logs all the calls to your functions. It will make your var dump output look even prettier. All sorts of things like that it'll do for you. It's a really nice companion to have.
Another alternative is DBG. You can go to their website and look up more information about that. And then the last one is Fire.PHP, 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. Now 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 permanantly.
Get unlimited access to all courses for just $25/month.Become a member