How to debug PHP scripts ? Last Updated : 25 Jun, 2019 Summarize Comments Improve Suggest changes Share 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 Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Like