SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to trace PHP code?

    I'm trying to figure out what some PHP code is doing. In particular, I need to see inside the structure of some of these PHP object variable. I'm doing

    HTML Code:
    print_r $someStuff;
    but it blocks program flow when it displays things to the page. And I see it display "Array()" on a white page. What does that mean? An empty array?

    How I can do a print_r to a log file?

    Or is there a better way to examine what goes on in PHP code?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Use an editor or IDE that has PHP debugging capabilities.
    I use Komodo IDE with xdebug.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Except, I'm debugging code on the server (test application on a test server on a shared webhost). I don't have a local PHP development environment. I have to use things like debug_backtrace(), var_dump, etc. But they all seem to print to page.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Use remote debugging?
    http://xdebug.org/docs/remote
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use the following lines which explode out the array and show you what is contains in a easy to read format ( well to me)

    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
    just insert, and doesnt seem to stop code running either.

    hope it helps

  6. #6
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/manual/en/function.print-r.php
    "If you would like to capture the output of print_r(), use the return parameter."
    After which you could dump it to a log file.

    But do yourself a favor and setup a local development machine.

  7. #7
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I have learned...

    a) Set up a local dev environment with Komodo IDE and xdebug.

    b) But in the mean time, I can do ...

    Code:
    $debugsomeStuff = print_r($someStuff,true);
    error_log($debugsomeStuff, 0);
    and the stuff will show up in my error_log at the root of my application.

    Thanks everyone.

  8. #8
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by artcoder View Post
    ... my error_log at the root of my application ...
    Path to error_log is based on server and ini settings.

    Here is what I use:

    Code:
    @ini_set('log_errors', 'On');
    @ini_set('display_errors', 'Off');
    @ini_set('error_log', LOGS_DIR . '/error_php_' . date('Ymd') . '.log');
    as you can see, I use ini_set to set path and file name of error log. In my default setting errors goes to Apache error log.

  9. #9
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What file to you put those @ini_set?

    In php.ini?

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by artcoder View Post
    What file to you put those @ini_set?

    In php.ini?
    No, those function calls would go in your PHP script, or entry-point for your application.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •