The key to becoming a great developer is learning how to troubleshoot effectively.
console.log() for printing things into the console within dev tools so you can tell whether a function or variable is working correctly.
In PHP — you can use things like
print_r() to do something similar.
In this article, I will go further with examples of how you can quickly and effectively troubleshoot in any situation you might find yourself in.
console.log() is hands down your best friend when it comes to figuring out problems.
Are the variable settings? Is it a string or an integer? Is it an object or an array? Is it NULL?
console.log() it, and it'll keep you from aimlessly guessing.
If you are using a JS framework like Vue or React — you'll undoubtedly find a ton of value in downloading and installing the following Chrome extensions.
Once installed — you'll need to close and reopen your Chrome dev tools to see the new panel. Within this panel are loads of helpful tools that allow you to see what's happening in your Vue and React app.
There are tons of fancy environment setups for getting error logs to print out on the screen. However, two extremely powerful functions allow you to diagnose what might be happening with your code quickly.
The for one is
var_dump(), which can be a little misleading because it does more than handle variables. It also handles functions too!
'Duder McDuderson' var_dump($name);$name =
Passing a variable function or object in
var_dump() — the next time you refresh your webpage — it will display what's happening.
I'll often use it when setting up loops and conditional with something like
var_dump('test') to check if the conditional logic works** the way I want it to.
The other function —
print_r() — does the same, but in some environments, it will print the data out in a human-readable way which can be extremely beneficial when working with arrays and objects.
When you find yourself scratching your head — do not hesitate to reach for any of these built-in functions. They will help you save loads of time and get you through some frustrating moments as you're out there building out there in the wild.