Debugging is a process of finding and reducing number of bugs in your code. An unavoidable and not particularly fun part of applications development. There are multiple approaches for debugging PHP code.
Before debugging, you need to prepare your development environment to show meaningful errors. Make sure to debug only in development environment and not production.
Turn on error reporting and set the appropriately useful error reporting level, either in the PHP code:
<?php // Turninig on errors in PHP code directly ini_set('display_errors', 'On'); error_reporting(E_ALL);
Or better yet, in the
php.ini file for development environment:
display_errors = On log_errors = On error_reporting = E_ALL
These are caused by a typo in your code. Examples include missing semicolons, quotation marks, incorrect variable definitions and so on.
Warnings will not break the execution of the script like syntax errors. Warnings are to notify you when you’ve made a mistake somewhere in your code but when the script can still execute.
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/project/includes/connect.inc.php on line 2
Notices will not break the execution of the script (just like with Warnings), but they are important pieces of information for debugging your application.
These errors are quite simple to debug, due to the script will cease execution when a fatal error occurs. Example of fatal errors include undefined functions, classes and so on.
Fatal error: Call to undefined function bootstrap() in /var/www/project/index.php on line 23
<?php $date = '2015-04-18'; $start = DateTime::createFromFormat('Y-m-d', $date); die(var_dump($start));
That way you can end execution of the script and check if output of the variable value matches what you expect it to be.
Xdebug - A Swiss-Army knife of PHP, Xdebug is a debugger
and profiler that can be used for more advanced debugging than simply using the
print_r functions. It is one of the most commonly used PHP
debuggers. Numerous IDEs also utilize it to provide you with a PHP debugging
Phpdbg is a default interactive PHP debugger integrated directly in the PHP out of the box. It is implemented and distributed as a SAPI module (just like the PHP CLI interface).
You can debug better by using the
dump() function of the
Symfony VarDumper component
than is otherwise possible with using
<?php require __DIR__.'/vendor/autoload.php'; // This is some variable which you want to check and debug. $someVar = '...'; dump($someVar);
The Zend Debugger is a PHP extension that should be installed on your Web server in order to perform optimal remote debugging and profiling using Zend Studio.
FirePHP is a debugger that enables you to log to your Firebug Console using a simple PHP method call.
<?php FB::log('Log message'); FB::info('Info message'); FB::warn('Warn message'); FB::error('Error message');