Change language

How do I debug PHP scripts?

When we write huge lines of code in PHP and then some kind of error occurs, then removing that error can be a feature of the task. Some common mistakes a programmer makes when programming in PHP:
  • Missing semicolon ";" and the closing brackets "}". 
    To debug the above errors, it is very helpful to use a good PHP ideal, as it will suggest a closing parenthesis "}" and the end of the ";" operator.
  • Wrong spelling of the variable name. Remember that $var! = $Var, as we know PHP - it is case sensitive.
  • Using "=" instead of "==" (assignment operator and equal operator)
    Example :
    if ($a = $b ) {// Statement} 
    This will always be True because there is never an error when assigning one variable to another.
  • Missing quotes in SQL statements like like "" and "". This is a very common and common mistake that occurs when programming in PHP. To fixerrors of this kind, always use mysqli_error ($con)with echo to see what error you are making in SQL statements, where $con - this is the connection variable you are using. 
    Example :
    if (! mysqli_query ($conn, $sql)) {echo "Error:". $sql. "
    ". mysqli_error ($con); }
  • If your PHP script does not produce any results while running, make sure "display_errors" is enabled in your php.ini file.
  • "Parsing error" - this error occurs when your code is not understood by PHP. This error usually occurs with a syntax error.
  • "Not understanding isset() behavior" - despite its name, isset() not only returns false if the element does not exist, but it also returns false for null values. This behavior is more problematic than it might seem at first glance, 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 supposedly wanted to check if keyShouldBeSet is set to $data. But, as mentioned, isset ($data [’ keyShouldBeSet ’]) will also return false if $data [’ keyShouldBeSet ’] was set but was set to null. the above logic is wrong.
  • One of the common mistakes is not to close PHP before using HTML commands. So always close PHP with "?>" and then write the HTML code and after completion of the HTML code, use "
  • PHP Debugging Tools:PHP code can be debugged using one of the many debugging tools to connect debugger client.PhpStorm works with debugging utilities such as Xdebug and ZendDebugger. Since we are a polyglot (knowing or using several languages), we need an IDE that supports multiple languages. Xdebug with Visual Studio has been used in the past, so let’s see how to set it up using VS Code.The debug server setup is the same, but each client (IDE or CLI) will have slightly different settings. See, the debug server (Zend extension) opens a port, and the client communicates with the server through that port. It’s just a matter of configuring and installing the correct components.Here are the steps to programming in PHP:
    • Check for PHP extensions in VS Code.
    • Install the extension PHP Debug.
    • Click reload to reload VS Code.
    • Install Xdebug. PHP Debug Extension for VS Code - it is only integration with Xdebug. If we install PHP 7.0, then it should get the correct version of Xdebug from the download page.
    • Now that you have the correct version, put it in the PHP / ext directory.
    • Next you need to configure PHP to use the extension and enable remote debugging. Add the following configuration to the php.ini file that is specified 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 configure the PHP server to use XDebug. The steps are the same, no matter which IDE you are using.
    • Xdebug opens an HTTP port so your debugger can connect. The client still needs to be configured to connect and use the debug protocol.
    Finally, configure VS Code to connect to Xdebug. There are a few simple steps and then the attachment is automatic.
  • Configuring your IDE:After installing Xdebug, you need to configure the IDE to connect to the debugger. In VS Code, this means adding a debug configuration. Fortunately, this happens automatically at the moment. These are just a few simple steps:
    • Switch to the debug view.
    • Click on the engine to open the languages ​​menu.
    • Select PHP. Visual Studio Code will create a default configuration.
    • Restart the PHP server. We have to install another extension called "PHP Server" that makes this easy. Use the context menu (right click) to control the PHP server.
    • This puts the IDE in a state ready to connect to Xdebug. Communication with the debugger takes place over the TCP port on the debug server. Xdebug uses the DBGp protocol by default on port 9000.
  • Attaching a debugger.The PHP Debug extension for VS Code creates a launch.json file. This file is placed in the .vscode directory at 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}]} 
    This adds two startup configurations. They are available in debug mode. We can either connect to a running server or start a new one with the current script. Since I already have phpinfo running, I’ll start by selecting Listen for XDebug to connect to this server. Once connected, you will see the debug toolbar. Most debuggers have a similar control mechanism that allows you to start, stop, start, and restart the debugger.
  • Toggle error reporting level:PHP has several ways to customize error reporting ... You can use php.ini file and access it. Otherwise you can use htaccess config. If you cannot use the config files, you have the option to change the values ​​using a script. The combination of settings will allow you to get the correct error logging levels. You will want to consider the following options:
    • error_reporting sets the logging level.
    • E_NOTICE is useful during development as it will tell you about defects such as unassigned variables.
    • display_errors is used to display error messages.
    • display_startup_errors should only be used for debugging.
    • log_errors and error_log work together to send errors to a log file. Do this in production rather than showing them to end users.
    • The PHP manual describes these options in more detail and provides more information.

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method