New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Debugging and troubleshooting

From: PHP with MySQL Essential Training

Video: Debugging and troubleshooting

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.

Debugging and troubleshooting

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.

Show transcript

This video is part of

Image for PHP with MySQL Essential Training
PHP with MySQL Essential Training

131 video lessons · 31642 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 4m 8s
    1. Welcome
      1m 0s
    2. Using the exercise files
      3m 8s
  2. 15m 6s
    1. What is PHP?
      3m 52s
    2. The history of PHP
      2m 51s
    3. Why choose PHP?
      4m 10s
    4. Installation overview
      4m 13s
  3. 54m 53s
    1. Overview
      2m 33s
    2. Working with Apache Web Server
      6m 56s
    3. Changing the document root
      7m 24s
    4. Enabling PHP
      6m 16s
    5. Upgrading PHP
      3m 30s
    6. Configuring PHP
      10m 3s
    7. Installing MySQL
      5m 46s
    8. Configuring MySQL
      7m 24s
    9. Text editor
      5m 1s
  4. 31m 25s
    1. Overview
      3m 27s
    2. Installing WampServer
      5m 46s
    3. Finding the document root
      2m 24s
    4. Configuring PHP
      8m 12s
    5. Configuring MySQL
      5m 45s
    6. Text editor
      5m 51s
  5. 19m 12s
    1. Embedding PHP code on a page
      6m 43s
    2. Outputting dynamic text
      5m 55s
    3. The operational trail
      2m 27s
    4. Inserting code comments
      4m 7s
  6. 1h 18m
    1. Variables
      7m 50s
    2. Strings
      4m 38s
    3. String functions
      8m 54s
    4. Numbers part one: Integers
      6m 27s
    5. Numbers part two: Floating points
      5m 25s
    6. Arrays
      10m 0s
    7. Associative arrays
      6m 37s
    8. Array functions
      6m 33s
    9. Booleans
      3m 50s
    10. NULL and empty
      5m 15s
    11. Type juggling and casting
      8m 27s
    12. Constants
      4m 43s
  7. 27m 37s
    1. If statements
      6m 0s
    2. Else and elseif statements
      4m 16s
    3. Logical operators
      7m 30s
    4. Switch statements
      9m 51s
  8. 42m 15s
    1. While loops
      8m 41s
    2. For loops
      5m 59s
    3. Foreach loops
      8m 16s
    4. Continue
      8m 28s
    5. Break
      4m 8s
    6. Understanding array pointers
      6m 43s
  9. 37m 25s
    1. Defining functions
      8m 25s
    2. Function arguments
      5m 32s
    3. Returning values from a function
      7m 33s
    4. Multiple return values
      4m 53s
    5. Scope and global variables
      6m 2s
    6. Setting default argument values
      5m 0s
  10. 20m 18s
    1. Common problems
      3m 47s
    2. Warnings and errors
      8m 36s
    3. Debugging and troubleshooting
      7m 55s
  11. 57m 57s
    1. Links and URLs
      5m 33s
    2. Using GET values
      5m 35s
    3. Encoding GET values
      8m 41s
    4. Encoding for HTML
      9m 26s
    5. Including and requiring files
      7m 40s
    6. Modifying headers
      6m 45s
    7. Page redirection
      6m 43s
    8. Output buffering
      7m 34s
  12. 1h 3m
    1. Building forms
      7m 28s
    2. Detecting form submissions
      5m 59s
    3. Single-page form processing
      7m 57s
    4. Validating form values
      10m 40s
    5. Problems with validation logic
      9m 54s
    6. Displaying validation errors
      7m 23s
    7. Custom validation functions
      6m 28s
    8. Single-page form with validations
      7m 25s
  13. 28m 5s
    1. Working with cookies
      2m 49s
    2. Setting cookie values
      5m 55s
    3. Reading cookie values
      6m 1s
    4. Unsetting cookie values
      4m 51s
    5. Working with sessions
      8m 29s
  14. 48m 39s
    1. MySQL introduction
      6m 43s
    2. Creating a database
      7m 41s
    3. Creating a database table
      7m 42s
    4. CRUD in MySQL
      5m 48s
    5. Populating a MySQL database
      7m 32s
    6. Relational database tables
      6m 40s
    7. Populating the relational table
      6m 33s
  15. 56m 4s
    1. Database APIs in PHP
      4m 51s
    2. Connecting to MySQL with PHP
      7m 45s
    3. Retrieving data from MySQL
      8m 47s
    4. Working with retrieved data
      6m 12s
    5. Creating records with PHP
      6m 58s
    6. Updating and deleting records with PHP
      9m 6s
    7. SQL injection
      3m 5s
    8. Escaping strings for MySQL
      6m 45s
    9. Introducing prepared statements
      2m 35s
  16. 35m 58s
    1. Blueprinting the application
      7m 19s
    2. Building the CMS database
      5m 14s
    3. Establishing your work area
      4m 38s
    4. Creating and styling the first page
      4m 22s
    5. Making page assets reusable
      6m 36s
    6. Connecting the application to the database
      7m 49s
  17. 32m 49s
    1. Adding pages to the navigation subjects
      5m 58s
    2. Refactoring the navigation
      6m 7s
    3. Selecting pages from the navigation
      6m 2s
    4. Highlighting the current page
      5m 26s
    5. Moving the navigation to a function
      9m 16s
  18. 1h 45m
    1. Finding a subject in the database
      9m 48s
    2. Refactoring the page selection
      10m 52s
    3. Creating a new subject form
      6m 55s
    4. Processing form values and adding subjects
      11m 20s
    5. Passing data in the session
      9m 16s
    6. Validating form values
      9m 40s
    7. Creating an edit subject form
      8m 30s
    8. Using single-page submission
      7m 44s
    9. Deleting a subject
      9m 44s
    10. Cleaning up
      10m 37s
    11. Assignment: Pages CRUD
      4m 30s
    12. Assignment results: Pages CRUD
      6m 10s
  19. 39m 26s
    1. The public appearance
      8m 52s
    2. Using a context for conditional code
      11m 37s
    3. Adding a default subject behavior
      6m 9s
    4. The public content area
      5m 51s
    5. Protecting page visibility
      6m 57s
  20. 1h 3m
    1. User authentication overview
      4m 3s
    2. Admin CRUD
      8m 41s
    3. Encrypting passwords
      7m 26s
    4. Salting passwords
      5m 42s
    5. Adding password encryption to CMS
      11m 54s
    6. New PHP password functions
      3m 13s
    7. Creating a login system
      11m 28s
    8. Checking for authorization
      5m 48s
    9. Creating a logout page
      5m 40s
  21. 2m 4s
    1. Next steps
      2m 4s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.