DEBUGGING

HOW TO DEBUG CODE IN PHP

What is debugging in PHP?

Where does this strange name come from, and what is this really about?

Term debugging is well known to all developers – not only PHP ones. It is a common term for trying to fix the wrongly working script. I don’t want to examine why we call a software error a bug. If you are curious about the origins, check out this link.

Debugging is simply getting rid of errors from the software. When you write the code, it often turns out that something doesn’t work as planned.

It’s worth knowing how to deal with such problems. You don’t always know where the mistake is made. Sometimes these are trivial errors like missing semicolon at the end of the command or some typo in the name of the method. The better the editor for writing the code, the faster we will catch those trivial errors. Smarter editors even highlight errors immediately, underlining the syntax and suggest proper names. Check out recommended text editor in the PHP development environment article.

Worse, if the code looks correct, and instead of the expected effect on the screen a blank page appears. Depending on the environment configuration, interpreter can print out all errors and warnings on the screen or hide them from us. It’s common in working with PHP scripts that we don’t want to present raw errors to the user.

WHY SHOULD WE HIDE THOSE ERRORS?

First of all, it’s about revealing sensitive information about your server. Often, specific path and file names appear in errors. Sometimes even we can see a complete set of tools on which the application is running. This information is very valuable for the attacker, because every technology has its strengths and weaknesses. It’s easier to crack security if you know the specific version of PHP, the server (Apache or NGINX) and even Operating System on which everything is running.

Second, we don’t want to scare regular visitor of our site.

Such things as stacktraces and execution logs look very scary and suspicious. Non-tech person generally avoids such websites and never comes back.

That’s why when we publish the application on a public server, information about errors should be turned off. That’s when we deal with production environment.

However, when we create and work on an application, it’s good to know how code behaves. We need to track any error and should see as much information as possible in order to fix it. This is called development environment.

Usually we deal with obvoius errors or crashes. We get blank screen or PHP exceptions thrown where some HTML login form should appear. Those are relatively easy to find as they happen during regular use of application.

Sometimes issues are more sophisticated and harder to find. Sometimes they are just small typos in one of the names. Having error information automatically displayed on the screen is one thing. Sometimes we want to track how a particular variable is changing.

CHECK VARIABLE WITH VAR_DUMP

Let’s say we have an array and we add to it several rows from database. For some reason, not all of expected by us values are stored there. The final display effect is quite different from what we planned. Then var_dump  command comes in handy.

Those of you who spent some time programming in PHP know this command well.

Those who see it for the first time, remember it well. You’ll probably use it often.

Var_dump literally “dumps the variable on the screen” together with all its parameters (type, length, etc.). If it’s an array, it will also return values ​​of all elements of this array. If it is an object, it will return the field values ​​and theirs  access modifiers. A really useful tool if you want to know what exactly is kept in one of your variables (and why the heck it’s now what I wanted!).

Sometimes you try to display a variable while it’s never been initialized (in PHP it’s easy as interpreter itself will create this variable for us ad-

Comments

Popular posts from this blog

FIRST LINE OF CODE IN PHP

VARIABLES AND CONSTANTS

SIMPLE PHP