How to debug PHP scripts ? Last Updated : 25 Jun, 2019 Comments Improve Suggest changes Like Article Like Report When we write huge lines of code in PHP and then some error occurs then removing that error might be a heck of the task. Some basic errors that programmer do while programming in PHP which are: Missing Semicolon ";" and closing brackets "}". To debug the above errors, using a good PHP ide will be very helpful as it will suggest the closing bracket "}" and end of statement by ";". Misspelling a variable name. Remember $var != $Var as we know, PHP is a case sensitive language. Using "=" instead of "==" (Assignment operator and Equal operator) Example: if($a = $b) { // Statement } This will always result in True as it is never an error to assign one variable to another. Missing quotes in SQL queries like ' ' and " ". This is a very common and frequent error that occurs while PHP programming. To debug this kind of error always use mysqli_error($con) command with echo to see what error you are doing in SQL statements where $con is the connection variable that you are using. Example: if (!mysqli_query($conn, $sql)) { echo "Error: " . $sql . "" . mysqli_error($con); } If your PHP script produces no output while running then make sure that "display_errors" is set to on in php.ini file. "Parse Error" - This error occurs when your code is not understood by PHP. This error generally occurs with a syntax error. "Misunderstanding isset() behavior" - Despite its name, isset() not only returns false if an item does not exist but also returns false for null values. This behavior is more problematic than it might appear at first and is a common source of problems. Example: $data = fetchRecordFromStorage($storage, $identifier); if (!isset($data['keyShouldBeSet']) { // do something here if 'keyShouldBeSet' is not set } The author of this code presumably wanted to check if keyShouldBeSet was set in $data. But, as discussed, isset($data['keyShouldBeSet']) will also return false if $data['keyShouldBeSet'] was set, but was set to null. So the above logic is flawed. One common error is missing PHP closing before using HTML commands. So always close PHP with "?>"and then write HTML code and after ending HTML code use "<?php" to again start php coding. PHP debugging tools: PHP code can be debug using one of many debugging tools to attach a debugger client. PhpStorm works with debug utilities like Xdebug and ZendDebugger. Being a polyglot (knowing or using several languages), we need an IDE that supports multiple languages. The Xdebug with Visual Studio is used in the past, so let’s see how to set it up with the VS Code. The debug server setup is the same, but each client (IDE or CLI) will have a slightly different setup. See the debug server (a Zend extension) opens a port, and the client communicates with the server through that port. It is just a matter of configuration and installing the right components. Here are the steps to doing PHP programming: Check for PHP extensions in VS Code. Install the PHP Debug extension. Click "reload" to reload VS Code. Install Xdebug. The PHP Debug extension for VS Code is only integration to Xdebug. If we install PHP 7.0 then it must get the right version of Xdebug from the download page. Now when you have the right version, put it in the PHP/ext directory. Next, you need to configure PHP to use the extension and allow remote debugging. Add the following configuration to the php.ini file that’s listed in PHP Info: ; set the extension path zend_extension="C:/Program Files (x86)/PHP/v7.0/ext/php_xdebug-2.6.1-7.0-vc14-nts.dll" ; allow remote debugging [XDebug] xdebug.remote_enable = 1 xdebug.remote_autostart = 1 It will set up the PHP server to use XDebug. The steps here are the same no matter what IDE you use. Xdebug opens an HTTP port so that your debugger can attach. The client still needs to be configured to attach and use the debugging protocol. Finally, configure VS Code to connect to Xdebug. There are a few simple steps and then attaching is automatic. Configuring your IDE: After installing Xdebug, you need to configure IDE to attach to the debugger. In VS Code, this means adding a debug configuration. Fortunately, it is automatic at this point. It’s just a few simple steps: Switch to the debug view. Click the gear to bring up the languages menu. Select PHP. Visual Studio Code will generate the default configuration. Reload the PHP server. We have to install another extension called "PHP Server" that makes this simple. Use the context menu (right-click) to control the PHP server. It puts the IDE in a state which is ready to attach to Xdebug. Communications with the debugger happen through a TCP port on the debug server. Xdebug uses the DBGp protocol through port 9000 by default. Attaching a debugger: The PHP Debug extension for VS Code generated a launch.json file. That file goes into a .vscode directory in the root of the project. { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Listen for XDebug", "type": "php", "request": "launch", "port": 9000 }, { "name": "Launch currently open script", "type": "php", "request": "launch", "program": "${file}", "cwd": "${fileDirname}", "port": 9000 } ] } It’s adding two launch configurations. Those are available in the debug view. We can either attach to a running server or launch a new one with the current script. Since I have phpinfo running already, I will start there by choosing Listen for XDebug to attach to that server. Once you are attached, you will see the debug toolbar. Most debuggers have a similar control mechanism which allows to start, stop, step, and restart debugger. Switching error reporting level: PHP has a few ways to configure error reporting. You can use the php.ini file and you have to access it. Otherwise, you might use the htaccess configuration. If you can’t use configuration files, you have the option of changing the values via a script. A combination of settings will get the right levels of error logging. You want to consider the following settings: error_reporting sets the level of logging. E_NOTICE is useful during development since it will tell about defects such as unassigned variables. display_errors is used to display the error messages. display_startup_errors should only be used when debugging. log_errors and error_log work together to send errors to a log file. Do this in production rather than displaying them to end users. The PHP manual spells out these settings in more detail and provides more information. Comment More infoAdvertise with us Next Article How to debug PHP scripts ? A anand27 Follow Improve Article Tags : Web Technologies PHP Similar Reads How to debug JavaScript File ? Debugging is essential because there are many errors that are not giving any kind of messages so to find out that we debug the code and find out the missing point. Example 1: Â Using console.log() Method In this, we can find out the error by consoling the code in various places. Using a console is on 2 min read Debugging Perl CGI Scripts Perl is a cross-platform, open-source computer programming language that is extensively used in both the commercial and private sectors. Perl is popular among Web developers due to its adaptable, ever-evolving text-processing and problem-solving capabilities. Although Perl was initially developed fo 5 min read How to Measure Script Execution Time in PHP? Measuring the script execution time in PHP is the time required to execute the PHP script. To calculate script execution time use clock time rather than the CPU execution time. Measuring script execution time is useful to check and improve the execution time for better application performance.Measur 2 min read How to Test PHP Code With phpUnit? Testing PHP code is a critical aspect of software development, ensuring that applications function as intended and maintain their integrity over time. PHPUnit stands out as a premier testing framework for PHP, empowering developers to create comprehensive test suites and validate their code effectiv 4 min read How to set up a PHP Console ? PHP is a backend server-side language of web development. In general, we don't need to take input from the console but sometimes we need to run a small function or block of code, for which we need to take input from the console. Requirements: Server: xampp or WampServer How to set up PHP Console ? S 1 min read How to debug code in ES6 ? In this article, we are going to learn how to debug a code in ES6. Debugging is the process of finding and resolving the problem or mistake in code due to which it doesn't work as expected. Debugging is an important part of programming that helps programmers to fix the bugs in the code. Checking an 2 min read How to terminate execution of a script in PHP ? In this article, we are going to discuss how to terminate the execution of a PHP script. In PHP, a coder can terminate the execution of a script by specifying a method/function called the exit() method in a script. Even if the exit() function is called, the shutdown functions and object destructors 2 min read How to enable error reporting in PHP ? In PHP, we can decide whether to show an error to end-users or not. You can enable or disable error reporting in PHP with three approaches: Approach 1: In the php.ini file, we can set the display_error parameter as on or off. The on means errors are displayed and off means no errors to display and r 3 min read How to Enable Shell Script Debugging Mode in Linux? Linux is a very popular operating system especially among developers and in the computer science world, as it comes along with its powerful ability to play with the kernel. Major Linux distributions (often referred to as Linux distros) are Open Source (Source Code available and can be modified on re 6 min read Like