How to Debug PHP Scripts for Beginners
Every developer, including myself, encounters programming mistakes that disrupt their scripts. When this occurs, confusion arises as one tries to identify the cause of the sudden malfunction. Several factors can lead to PHP script failures, with the most prevalent errors being:
Absent semicolons at statement ends
Omitted closing brackets when needed
Overlooked variable declarations and initializations
Inconsistent use of single and double quotes
With php errors, you can use a debugger which helps in identifying and rectifying errors, issues, and bugs within PHP code
What is a PHP Debugger?
A PHP debugger is a software tool that helps developers find and fix errors and bugs in PHP code. It offers features to troubleshoot and understand how PHP scripts run. Debuggers are essential for programmers to make code more reliable and efficient by simplifying error identification and resolution. Debuggers come in handy when dealing with large and complex php scripts and applications.
Enable PHP Error Display in php.ini Settings
Look at the provided PHP script:
<?php
/*
Script 1.0
This php script has an error!
*/
$first_name = "Kelvin"
echo $first_name;
?>
When the app runs, the browser displays an error:
(Parse error: syntax error, unexpected token "echo" in C:\wamp64\www\tbb\debugger\index.php on line 4)
If your PHP code doesn't show clear errors, it might be due to incorrect error_reporting setup or 'display_errors' turned off in php.ini. Check php.ini to ensure error_reporting is right and display_errors is enabled.
Opening the php.ini File
To access the php.ini file, click your WAMP icon, select PHP, and then choose php.ini as depicted below:
The php.ini file opens in the IDE you picked when installing WAMP on your Windows PC. For instance, I use Notepad++ as my default IDE. Once inside the file, use Ctrl + F to open the 'Find' dialog. Search for 'error_reporting' to locate the line. In my case, the option is on the line displayed below
If you're on a development server (localhost), ensure the value is set to E_ALL, indicating all errors, warnings, and notices, including coding standards.
On a production server (for end users), set the value to E_ALL & ~E_DEPRECATED & ~E_STRICT, showing all errors except those for future PHP versions and coding standard warnings.
Thereafter, check the line on 'display_errors' and ensure its been set to on, which is usually the default on development servers, such as localhost.
Error Warnings on Visual Studio (VS) Code
Visual Studio Code, commonly referred to as VS Code, is a free, open-source code editor developed by Microsoft. It's designed to provide developers with a lightweight yet powerful environment for writing and debugging code across various programming languages and frameworks.
The editor comes with built-in debugging support for various programming languages, including PHP. Debugging can be done directly within the editor, allowing developers to set breakpoints, inspect variables, and step through code.
Consider our earlier php Script 1.0. When the code is written on VS Code editor, it is represented as follows:
By default, VS Code underlines syntax errors with red. Once you encounter the underlined line, you can check it or the line preceding it for errors.
When we run the debugger on the file, the debug console outputs:
Using php_error.txt Log File
The php_error.txt log file is a text document that records errors, warnings, and notices generated by PHP scripts. It provides insights into issues that occur during the execution of PHP code, helping developers identify and diagnose problems. This log file is particularly valuable for troubleshooting and debugging PHP applications, as it gives a detailed account of what went wrong, such as syntax errors, runtime issues, or unexpected behaviors.
As a developer, you can review the log file to understand the context of errors and take corrective actions to ensure the smooth functioning of their PHP applications.
To get the php_error.txt, go to C:\wamp64\logs if you are on a Windows X64 system or C:\wamp\logs if you are using a Windows X86 system. Alternatively, you can click your WAMP icon, select PHP, and then choose PHP error log.
Looking at the last line, we can see our error indicated as:
<?php
[25-Aug-2023 09:23:48 UTC] PHP Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\wamp64\www\tbb\debugger\index.php on line 4
[25-Aug-2023 10:10:29 UTC] PHP Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\wamp64\www\tbb\debugger\index.php on line 4
[25-Aug-2023 10:12:58 UTC] PHP Parse error: syntax error, unexpected token "echo" in C:\wamp64\www\tbb\debugger\index.php on line 4
?>
In the logs directory, you can find other crucial logs including apache_error, mysql, and mariadb logs.