Debugging & Testing
Debugging and testing are interdependent processes. The purpose of testing is to identify if there are any mistakes in a program's source code. Similarly the purpose of debugging is to locate that mistake and fix it. Debugging helps the developer to determine the root cause of technical error in order to fix it.
Some Examples of Coding Errors are as follows :
1. Syntax Error
2. Logic Error
3. Semantic Error
5. Runtime Error
6. Ignoring adopted conventions in the coding standards
7. Ignoring the naming conventions
8. Not adding conditions around a piece of code
9. Skipping a check for Error handling
PHP Error reporting options ref
// Turn off all error reporting
error_reporting(0);
//------------------------------------------------------------------------------------
// 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);
//------------------------------------------------------------------------------------
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
//------------------------------------------------------------------------------------
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
//------------------------------------------------------------------------------------
// Report all PHP errors
error_reporting(-1);
//------------------------------------------------------------------------------------
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
X debug
Xdebug is an extension of PHP which helps developers in debugging and smooth development of their projects to carefully watch for errors and solve them.
- It upgrades PHP’s var_dump() function.
- It also adds stack traces for Notices, Warnings, Errors, and Exceptions.
- Xdebug allows us to add breakpoints and stop the code execution at every breakpoint so that you can see variables output in only one iteration of code.
- Without Xdebug, you must refresh the page to see the output again.
You can also set breakpoints independent methods to see which one is causing issues. Also, you can add as many breakpoints as you want in the code, and then use PHP debugger to check each code line by line.
Download : xdebug.org
Thread Safe(TS) and Non Thread Safe(NTS)
Apt form of Module must be downloaded
edit php.ini (for wamp there are 2 ini files, use C:\wamp64\bin\php\php7.4.9\phpForApache.ini)
For suggestion copy phpinfo(whole screen ) and paste it intoXdebug wizard
zend_extension="c:/wamp64/bin/php/php7.4.9/zend_ext/php_xdebug-2.9.6-7.4-vc15-x86_64.dll"
xdebug.remote_enable = off
xdebug.profiler_enable = off
xdebug.profiler_enable_trigger = Off
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir ="c:/wamp64/tmp"
xdebug.show_local_vars=0
Notepad++
-
Download the latest version of Xdebug Plugin DBGp.
-
Modify php.ini
[xdebug] zend_extension_ts="php_xdebug-3.1.2-7.4-vc15-x86_64.dll" ;xdebug.mode allowed are : off develop coverage debug gcstats profile trace xdebug.mode = develop,debug xdebug.output_dir ="c:/wamp64_v2/tmp" ;xdebug.show_local_vars=0 xdebug.log="c:/wamp64_v2/logs/xdebug.log" xdebug.log_level=7 xdebug.profiler_output_dir = "C:/wamp64_v2/tmp/xdebug" xdebug.profiler_output_name = "cachegrind.out.%p" xdebug.profiler_enable = 0 xdebug.profiler_append=0 xdebug.extended_info=1 xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.remote_host=127.0.0.1 xdebug.client_port=9000 xdebug.remote_port=9000 xdebug.idekey=XDEBUG_ECLIPSE xdebug.remote_log="c:/wamp64_v2/tmp/xdebug/xdebug_remot.log" xdebug.show_exception_trace=0 xdebug.show_local_vars=9 xdebug.show_mem_delta=0 xdebug.trace_format=0
-
Create 'xdebug' folder in "c:/wamp/tmp/"
-
Restart Apache
-
Configure DBGP Plugin setting appropriately in Notepad++
-
Make sure ports are set correctly in launch.json and php.ini
xdebug.client_port=9000 xdebug.remote_port=9000
-
Check PHP info for
idekey
. Browser plugins(mentioned below) have default setting likexdebug.idekey=XDEBUG_ECLIPSE
some havexdebug.idekey=xdebug
Both must be same in php.ini and respective setting for notepad++ or VSCode
VS Code
Install PHP Debug extension by Felix Becker
You can do so by selecting the extensions tab or CTRL+SHIFT+X) in VSCode and searching for PHP Debug
- Click on "Run & Debug" icon in the left side
- Select "Add Configuration"
- Choose PHP
launch.json
will be created in theroot directory opened in VS Code
- Add "runtimeExecutable": "C:\wamp64\bin\php\php7.4.9\php.exe" below "port"
- Make sure ports are set correctly in launch.json and php.ini
xdebug.client_port=9000 xdebug.remote_port=9000
-
Check PHP info for
idekey
. Browser plugins(mentioned below) have default setting likexdebug.idekey=XDEBUG_ECLIPSE
some havexdebug.idekey=xdebug
Both must be same in php.ini and respective setting for notepad++ or VSCodeYour default Browser
- install appropriate plugin for Xdebug (Firefox / Chrome)
- VSCode Code Live server, make sure you installed it.
- Open the Command Pallete by pressing F1 or ctrl+shift+P and type Live Server: Open With Live Server to start a server or type Live Server: Stop Live Server to stop a server.
- whne you have just installed
- Open the Command Palette. Ctrl + Shift + P.
- Then type: Reload Window.
Profiling
https://xdebug.org/docs/profiler
Output can be analysed with PHP Storm
or QCacheGrind
-d xdebug.profiler_enable=1 -d xdebug.profiler_output_dir="E:/projects/PHPStormPjts/workshopComposer/workshop/06_Debugging/profilerOutput"
Upgrading from Xdebug2 to Xdebug3
https://xdebug.org/docs/upgrade_guide
xdebug version 2
php.exe -d xdebug.profiler_enable=1 -d xdebug.profiler_output_dir="< output dir fill path>" < php file full path>
xdebug version 3
php.exe -d xdebug.mode=profile -d xdebug.output_dir="< output dir fill path>" < php file full path>
|
|