Comprehensive Guide to Debugging PHP Code Print

  • 0


Debugging plays a crucial role in web development, and PHP is no exception. A minor mistake in your PHP code can lead to unexpected outcomes and even crash your website. However, enabling debugging in PHP can help you identify these errors, understand what's going wrong, and ultimately correct it.

This article will guide you through the process of enabling debugging in PHP code via the cPanel interface, a popular web hosting control panel.

Steps to Enable Debugging

  1. Log in to cPanel: The first step is to access your cPanel account. This can typically be done by appending "/cPanel" to your website's URL, though it may vary depending on your web hosting provider.

  2. Navigate to File Manager: Once you are logged in, look for the 'File Manager' under the 'Files' section and click on it.

  3. Locate Your PHP File: In the 'File Manager', navigate through the file directory to locate the PHP file you want to debug. The files are usually located in the public_html folder, although the exact location may depend on your specific configuration.

  4. Edit the PHP File: Right-click on the desired PHP file and select 'Edit'. A dialogue box may pop up asking for encoding verification. You can safely click 'Edit' to proceed.

  5. Enable Debugging: In the editor, insert the following lines of code at the beginning of your PHP file. These lines enable PHP's built-in error reporting feature:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

  1. These commands direct PHP to display all errors, warnings, and notices, including those triggered during the script's startup.

  2. Save Changes: After adding these lines, click on 'Save Changes' in the top right corner of the editor.

  3. Test Your PHP File: You can now revisit your website or the specific page where the PHP file is in action. If there are any errors, warnings, or notices in your code, they will now be displayed on the page.

Please remember to remove or comment out these debugging lines once you're finished troubleshooting to prevent users from seeing these errors.

Comprehensive Guide to Debugging PHP Code

Debugging is an integral part of any development process and it's no different for PHP programming. When developing PHP applications, it's inevitable that you will encounter errors and anomalies that need to be fixed. In this article, we'll explore various methods that can be used to debug PHP code, each with its unique approach and use-cases.

1. Using PHP's Built-in Functions

PHP comes with several built-in functions that can assist in debugging. These include functions like var_dump(), print_r(), error_reporting(), and debug_backtrace(). These functions can be used to display the type, structure, and value of variables.

For instance, var_dump() can be used like this:

$myArray = array("apple", "banana", "cherry");

This will output the array's structure, data type, and values.

2. Debugging with Xdebug

Xdebug is a powerful PHP extension that provides a variety of debugging and profiling capabilities. It can give detailed error messages, function traces, code coverage analysis, and more. Xdebug can be installed and configured to work with your preferred Integrated Development Environment (IDE) or text editor.

To use Xdebug, you'll need to install and configure the extension, typically by modifying your php.ini file and restarting your web server. Then, integrate Xdebug with your preferred IDE or use it via the browser.

3. Using an IDE with Debugging Capabilities

Many Integrated Development Environments (IDEs) come with built-in PHP debugging capabilities. Examples include PHPStorm, Visual Studio Code (with the right extensions, such as PHP Debug), and NetBeans. These IDEs allow stepping through the code, pausing execution, inspecting variables, and more. These capabilities make it easier to find and fix issues in your code.

4. Debugging with Error Logs

Error logs contain a record of runtime errors encountered by the PHP interpreter. These logs can be incredibly insightful for debugging your PHP application.

The location of these logs depends on your server setup. If you have access to your php.ini file, you can configure where errors are logged using the error_log directive. Additionally, make sure the log_errors directive is set to On. This will enable error logging.

A typical error log entry might look something like this:

[01-Jul-2023 18:35:12 UTC] PHP Fatal error: Uncaught Error: Call to undefined function myFunction() in /home/mywebsite/public_html/myscript.php:50
Stack trace:
#0 {main}
thrown in /home/mywebsite/public_html/myscript.php on line 50

This log entry tells us that an undefined function was called, the file in which the error occurred, and the exact line that triggered the error.

5. Creating Custom Log Files

Another useful technique for debugging is creating custom error logs within your project. This lets you isolate and access your logs easily without having to sift through system files.

Here's a simple function for writing custom logs:

function writeLog($message) {
$file = 'logs/errorlog.txt'; // Specify the log file location
$current = file_get_contents($file); // Get current content of the file
$current .= "[" . date('Y-m-d H:i:s') . "] " . $message . "\n"; // Append the message to the file content
file_put_contents($file, $current); //

// Write the contents back to the file }

// Now you can use the function whenever you catch an exception or an error try { // Some code that can potentially throw an exception } catch (Exception $e) { writeLog("Caught exception: " . $e->getMessage()); }

In this example, the `writeLog` function writes the provided message to a file named `errorlog.txt` within a `logs` directory. The function appends a timestamp and the new error message to the existing content of the file. You can use this function in your catch blocks to log any exceptions during execution.

The `logs` directory and `errorlog.txt` file must exist and have the appropriate permissions. You can create the directory and file manually, or you could add code to your script to create them if they don't exist.

6. Debugging in cPanel using PHP Error Logs

If you're using cPanel to manage your server, you can easily access your PHP error logs. cPanel's error log provides detailed information about the last 300 errors that occurred on your website.

1. To access the error logs, login to cPanel.
2. In the "Metrics" section, click on "Errors".
3. A new page will appear showing the last 300 errors.

Another option available in cPanel is to enable the display of errors directly in your PHP scripts using the `php.ini` file. Here's how:

1. In cPanel, go to the "Software" section and click "MultiPHP INI Editor".
2. Select The domain
3. Locate and click on "display_errors" and switch it from "Off" to "On".

Remember to turn off the display of errors on live sites to avoid revealing sensitive information to the public.


Debugging is an essential part of the development process, and having an array of tools and techniques at your disposal will make solving issues more efficient. From using built-in PHP functions, utilizing powerful extensions like Xdebug, leveraging your IDE's debugging features, analyzing error logs, to creating custom log files, and using cPanel's capabilities — each method has its advantages and can help streamline your debugging process.

Let's dive into more details about the PHP built-in functions: `var_dump()`, `print_r()`, `error_reporting()`, and `debug_backtrace()`, with examples:


The `var_dump()` function in PHP displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.

Here is an example:

$array = array("foo", "bar", "baz");

This will output:

array(3) {
string(3) "foo"
string(3) "bar"
string(3) "baz"

2. `print_r()`

The `print_r()` function in PHP prints human-readable information about a variable in a way that's readable by humans. It's similar to `var_dump()`, but presents the information a bit differently.

Here's an example:

$array = array("foo", "bar", "baz");

This will output:

[0] => foo
[1] => bar
[2] => baz

3. `error_reporting()`

The `error_reporting()` function in PHP is used to set the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script.

If the optional level is not set, error_reporting() will just return the current error reporting level.

Here's an example:

// Turn off all error reporting

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);


The `debug_backtrace()` function in PHP generates a backtrace snapshot. This can be useful for debugging your code, as it can tell you what code called what other code.

Here's an example:

function myFunction() {

function myTrigger() {


This will output something similar to:

array(2) {
array(4) {
string(18) "/home/user/test.php"
string(10) "myFunction"
array(0) {
array(4) {
string(18) "/home/user/test.php"
string(10) "myTrigger"
array(0) {

Each of these functions plays a crucial role in debugging PHP, and understanding how and when to use them can greatly improve your debugging capabilities.

Was this answer helpful?

« Back