Results 1 to 2 of 2

Thread: Methods of debugging in PHP

  1. #1
    Join Date
    May 2008
    Posts
    109

    Methods of debugging in PHP

    PHP is well endowed with debugging functions, it will be difficult after that to find an excuse not to try to correct your mistakes by yourself ;-)

    All these methods relate to the posting of information on variables or the context in a script. They also relate to bad habits that must be taken carefully ...

    1. Information on a variable

    echo

    The first function, the most basic to display the value of a variable is of course "echo". It can display an integer, a string or a real number.

    It is a habit to a SQL does not? Then store it in a variable and then display it with an echo. You can find out if the problem comes from a variable that has a value inconsistent or null.

    You can also test your query directly by copying and pasting that is displayed on your web page.
    For example, if you use MySQL, you can test your query with PhpMyAdmin or the Client Official (via the Linux console or a DOS window in Windows).

    print_r ()

    The echo function is useful but insufficient in some cases. It can not display a table or the characteristics of an object. For this, print_r is more advanced. This feature will display the value to an integer, a string or a real and present you in a structured way a table or an object.

    var_dump () / var_export ()

    More verbal than its previous function var_dump () will give you the type of a variable or fields of a table etc ... and their exact content and, for strings, the number of characters. If the variable contains a value "or false vacuum" as 0,''(empty string), Null or False, you can type and the exact value.

    Var_export () is somewhat similar but present information in the form of PHP code.

    Redirect the output (not display on the screen)

    What I call here the "exit" are the data that normally appear on the screen.
    You may need to record data reported by var_dump print_r or in a file rather than the screen or web page.
    Say you want to include this information in a file named test.log
    You will have to keep in mind the output using the function ob_start () and at the end of your script, save the output in a variable with ob_get_contents () and then delete the data to the screen with ob_end_clean ()
    It finally enough to write this data in the file.

    Here is what happens:

    I have an array called $ tab, I want to record its description in the file test.log rather than the screen:

    Code:
    <? 
      ob_start (); 
      $ array = array (1 => 'test', 
    	  2 => 'test2'); 
      var_export ($ tab); 
    
      $ tab_debug = ob_get_contents (); 
      ob_end_clean (); 
    
      $ file = fopen ( 'test.log', 'w'); 
      fwrite ($ file, $ tab_debug); 
      fclose ($ file); 
      >
    Now, details of the $ tab are saved in the file and nothing has been displayed on the screen or on the web page.

  2. #2
    Join Date
    May 2008
    Posts
    109

    Re: Methods of debugging

    2. "Backtrace" or report context

    A backtrace, or report context (if someone is better as a translation, please ...) you will retrace the path through the call inclusions functions and file to arrive at a given point in the code.

    We have a first page (fonctions.php)

    Code:
    <? php  
    
      function b () 
      ( 
    	  var_dump (debug_backtrace ()); 
      ) 
      a function () 
      ( 
    	  b (); 
      ) 
    
      a (); 
    
      >
    Then a second file that call fonctions.php (we call main.php):

    Code:
    <? php 
      include 'fonctions.php'; 
      >
    Here's what you get:

    Code:
    array (3) ( 
        [0] => 
        array (4) ( 
          [ "file"] => 
          string (29) "/ fonctions.php" " 
          [ "line"] => 
    
          int (10) 
          [ "function"] => 
          string (1) "b" 
          [ "args"] => 
          array (0) ( 
          ) 
        ) 
        [1] => 
        array (4) ( 
          [ "file"] => 
          string (29) "/ fonctions.php" " 
          [ "line"] => 
          int (13) 
          [ "function"] => 
          string (1) "a" 
          [ "args"] => 
          array (0) ( 
          ) 
        ) 
        [2] => 
        array (4) ( 
          [ "file"] => 
          string (30) "/ main.php" 
          [ "line"] => 
          int (3) 
          [ "args"] => 
          array (1) ( 
            [0] => 
            string (29) "/ fonctions.php" 
          ) 
          [ "function"] => 
          string (7) "include" 
        ) 
      )
    We have three tables, the analysis is done from the last to arrive until the first if we want to trace the path of code from the beginning.

    In each chart we have:

    function: the function that is called
    args: the arguments passed to this function (here, the path to fonctions.php)
    line: the row in the code
    file: the path to the php file being processed.

    In this long thread, to get to the point of tracing (debug_backtrace ()), we note that we change the function include in the main (main.php) followed by a () and by b () fonctions.php in the file.

    It may be useful to see how the code behaves in the order of call functions and value of parameters that are given as arguments.

    The function debug_backtrace () returns an array containing the report of context from where you've called.

    I have displayed directly with var_dump () but you can use as you want, store it in a variable, and so on.

    The function debug_print_backtrace (PHP 5) you can directly view the report.

    3. How to use crash reports

    PHP details of the errors on web pages (or the screen if you use PHP for an application that is not for web).
    Feel free to look in detail.

    You may have disabled the error report using the @ operator
    Use it sparingly, it is useful as it may, for example, you can not give information about your database in an error report, but still manage to think that this error will be displayed not.

    Similarly, reporting to PHP you report all errors (default usually), thanks to the error_reporting, you will help you find:

    Code:
    error_reporting (E_ALL);
    4. The global keyword

    There is PHP a keyword called "global".
    It lets you use a variable of global scope within a function.
    Flee this keyword! Do not use it, unless your life is at stake ...
    This feature makes the code unreadable, and debugging difficult to follow.

    5. The debugger ODA

    PHP also has advanced debugging grouped under the name of ODA.

Similar Threads

  1. JIT debugging problem - I've tried everything!
    By Lutrown in forum Windows Software
    Replies: 3
    Last Post: 05-04-2012, 03:48 AM
  2. Use Of Environment debugging
    By winni in forum Software Development
    Replies: 4
    Last Post: 09-01-2011, 10:19 PM
  3. HTTP debugging tools
    By Galbraith in forum Software Development
    Replies: 4
    Last Post: 26-02-2010, 06:12 AM
  4. What is Script Debugging
    By Aakaar in forum Software Development
    Replies: 3
    Last Post: 13-07-2009, 10:53 AM
  5. vb.net Debugging Software
    By Aagman in forum Software Development
    Replies: 2
    Last Post: 20-04-2009, 11:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,093,195.94936 seconds with 17 queries