Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we have a lot of the PHP fundamentals under our belt. In this chapter, we'll discuss ways to troubleshoot and debug your PHP code. And we'll start by looking at common problems and talk about the errors that PHP returns. And finally, look at some debugging techniques. Let's start out by looking at the common problem. One of the most frustrating problems that you can run into is getting a webpage that has no output on it at all. There's no real information there to go on. It just seems like the page is not working. The first thing you should do is try to access a basic HTML page.
Forget about PHP for a second. Take it out fo the equation. Make sure your web server is running. And that it's able to return HTML pages. That also helps to make sure that we're accessing the right directory in the file system that we think we are. Once we get that working, then we can bring PHP into the equation. And we can try to create a basic PHP page. And see if we can get our web server to process the PHP. Remember that we have that phpinfo function that we can use to drop into a simple PHP page. It will confirm that PHP is working and it also gives us lots of configuration information that can be useful.
Once we know that the web server's running, that it's finding files in the right place. That it can return those files and the PHP is running, then the problem is probably in our code. You want to make sure the display errors is turned on and configured properly. We're going to talk about that more in the next movie. You maybe encountering an error and PHP just isn't showing you that error so you're getting a blank page instead. Now, let's talk about some of the common errors that you might have created in your code that would cause those errors. The first and probably most common is just simple typos. Maybe you just misspelled a variable, maybe you commented out something that you didn't mean to comment out. Typos are just a way of life when you're doing programming. And you're going to have to get used to combing over your code very carefully. Very thoroughly, looking for mistakes that you've made. A very common mistake in PHP is missing the semicolon at the end of the line. Remember those semicolons are super important.
PHP is whitespace independent so it doesn't matter if we have returns. But the semicolons are what tell PHP when one command ends and another one starts. It's also super common to have missing closing braces or missing closing quotes. And that would be that you open a parentheses, but you don't close it. Especially once you start nesting parentheses inside of parentheses. It can get hard to keep track. Most code text editors either helpfully add that other parentheses for you when you create the first one. But they will often also highlight the opening parentheses when you type a closing parentheses. So, it helps you know what that parentheses is paired up with. So, you'll know when you have enough of them.
Another problem can be from having case sensitive variable names. Remember that something like myvar, all lower case, and myvar with an upper case V, are two different variables. That's why I think it's a good practice to pick one style, and stick with it. For me, that's why I like using all lower case. So that I never have to run into this. All my variables are always lowercase. And then, the last one that's super common especially among beginners, is confusing equals with double equals. And I don't mean confusing in the fact that you don't know the difference, but that you mistakenly use the wrong one.
So, if you have a simple if statement. If I have count equals 3, well in this case, I'm assigning the value 3 to count. So, if count was equal to 10 when it got to this statement, and I was checking to see is count equal to 3, well guess what? Now it is equal to 3. I meant to be checking it, but instead I set it. Now, these are the most common mistakes that people can make. But certainly, there are a lot of other things that can go wrong. Remember that the php.net website is great resource. And there's lots of documentation about how the different functions work as well as helpful user comments.
Below that that might be able to help you out with the problem you're having and the PHP community is quite large. So, there's all sorts of blogs and other documentation out there that you can search the internet and find that might help you address your problem.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98266 Viewers
61 Video lessons · 85559 Viewers
71 Video lessons · 69522 Viewers
56 Video lessons · 101854 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.