Debugging in PHP, the term “bug” was first used by Thomas Edison and make it popularized by Grace Hopper in 1947.
Introduction
Debugging is a very important part of programming and development. By using debugging we can find out errors and issues inside our script.
Debugging is a process that helps us to find errors in a quick manner. Sometimes debugging is time taking but it’s a part of our development process.
Using Built-in Error Reporting and Logging Functions
In PHP there are several built-in functions that can be used to display and log error messages. The most commonly used functions are error_reporting()
and ini_set()
.
1 2 |
error_reporting(E_ALL); ini_set('display_errors', 1); |
- The
error_reporting()
function used to set the error reporting to true(1) or false(0). If it is set as true then all the errors, warnings, and notices will show. - The
ini_set()
function sets the value of thedisplay_errors
configuration option. When set to 1, this option will display error messages on the screen.
In addition to displaying error messages on the screen, you can also log them to a file using the error_log()
function.
1 2 |
ini_set("log_errors", 1); ini_set("error_log", "/path/to/error.log"); |
The error_log
option sets the location of the error log file, and the log_errors
option enables error logging.
Using a Debugger Tool
- For debugging inside the PHP code we can use a debugger tool. There are two of the most popular options are Xdebug and Zend Debugger.
- Xdebug is a powerful tool that allows you to step through code, set breakpoints, and inspect variables at runtime.
1 2 3 4 5 |
<?php xdebug_break(); $a = 5; $b = 10; $c = $a + $b; |
Code Explanation
- When we will run the above code with Xdebug enabled, will stop execution at the
xdebug_break()
function call, allowing us to inspect the values of variables$a
,$b
, and$c
at that point in the execution. - Zend Debugger is also an important debugging tool for PHP. It provides similar functionality to Xdebug, including setting breakpoints, stepping through code, and inspecting variables at runtime.
Using Print Statements
Print Statements effective way to debug PHP code and use to print statements. This method involves adding statements to the code that display the values of variables at specific points at the time of execution of the program.
1 2 3 4 5 |
<?php $a = 5; $b = 10; print "Value of a: " . $a . " Value of b: " . $b; $c = $a + $b; |
The above code will display the values of the variables $a
and $b
on the screen.
Use the print_r()
function to output the contents of arrays and objects.
1 |
$my_array = array("apple", "banana", "cherry");<br>print_r($my_array); |
Use “var_dump()”
Use the “var_dump()” function to output the values of variables and check for any unexpected results.
1 2 |
$my_variable = "Hello World"; var_dump($my_variable); |
Use the “try-catch”
Use the “try-catch” block to catch and handle exceptions.
Example:
1 2 3 4 5 |
try { $result = $db->query("SELECT * FROM users"); } catch (PDOException $e) { echo "Error: " . $e->getMessage(); } |
Use the “set_error_handler()”
Use the “set_error_handler()” function to define a custom error-handling function.
Example:
1 2 3 4 5 |
function my_error_handler($errno, $errstr, $errfile, $errline) { error_log("Error: $errstr in $errfile on line $errline"); return true; } set_error_handler("my_error_handler"); |
Conclusion
Debugging is part of our web or software development process. Before starting the debugging of any source code, first, we need to understand the flow of the complete project. Understanding the flow helps to debug our code very efficiently.
Also, Read
- Set PHP Error Reporting Into The PHP File
- Complete Guide PHP Error Log
- Complete Guide About Create PHP With MySQL Connection