Oh, you’ve done it this time. You’ve always been confident about your technical skills and have worked your way up the ladder within your organization to work on their servers. You sit down to work on a reported issue– and you have to debug PHP code. PHP is a server-side programming language that you can use for a broad range of tasks, but you’ve never worked with it before this shift, and you’re at a loss as to how you should go about debugging PHP. You don’t even know where to begin.
Calm down, my sysadmin pal. We’ve developed this guide for such an occasion, so you’ve got nothing to worry about. We walk you through the PHP debugging process, beginning with standard debug actions that you can use when working with most languages and finishing with PHP-specific debugging tools that will let you cut through the red tape and figure out exactly what’s causing your server to act so sad all the time. Soon, you’ll be a PHP-coding wizard!
Output and Dump
To get started with PHP debug, you’ll want to know exactly what you’re dealing with as far as the world of PHP errors. There are some excellent third-party apps that you can use to debug PHP, such as Xdebug but, since you’re a brand-new server admin, still wet behind the ears and covered with packing foam, we’ll start you with nothing but PHP. That’s fine, though—PHP has lots of fantastic debugging tools to help you along the way.
You’ll want to output your values to STDOUT so that you can review it to check for errors. You can dump your output to the log using a couple of different commands; you can use var_dump ($var) to dump a specified variable, and you can use type and print_r ($var) to produce a human-readable variable dump. Use these commands to start building your log.
Step Through Your Code
Eventually, just scanning the logs won’t be enough to figure out what’s wrong with your PHP code. Sooner or later, you’re going to need to step through your code to find the issue. Stepping through code is standard practice in any programming environment or language. It allows you to watch the program in action and keep track of all of its functions. Stepping through your code is an effective way to pinpoint the exact point at which your program went sideways and started throwing errors.
Those PHP debug extensions we touched on earlier will come in handy to help you work your way through the code. Xdebug and other debuggers will allow you to build servers that can mimic your production servers’ environment. A good debug environment will allow you to do anything that you can do on your actual network there so that you can keep your bug squashing risk-free since you won’t have to worry about disrupting your company’s service while you get all Sherlock Holmes on the problem.
So, you can relax and breathe easy the next time you have to dig through your server’s PHP code to identify an issue. Use your logs and debuggers, and make sure to step through your program. In no time at all, your PHP code will be pristine and purring like a cute and logical kitten.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.