Open In App

How to debug PHP scripts ?

Last Updated : 25 Jun, 2019
Like Article

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)

    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.

    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.

    $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 
      ; allow remote debugging
      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:
        "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.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads