print out the expression as well as the value automatically, possible in PHP?

Discussion in 'PHP' started by winterheat, Sep 4, 2008.

  1. #1
    is this possible in PHP? such as

    debug_print($s);
    debug_print($iInterestRate);
    debug_print($date->now())
    ;

    print out:

    $s is Hello World
    $iInterestRate is 0.032
    $date->now() is 1250403294320


    it is possible in C and in Ruby... wonder if something like that is possible in PHP for easy debugging. Thanks very much!
     
    winterheat, Sep 4, 2008 IP
  2. Limotek

    Limotek Peon

    Messages:
    165
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi winterheat. I think I just responded to your question in another forum so I've copied the post for the benefit of anyone here that may want to know.

    --------------------------------------------------------------------------

    I'm not familiar with any predefined method that does this in PHP.

    A simple way would simply be to print manually such as:

    print '$foo is '+$foo;
    PHP:
    However, I'm assuming that as you're asking the question, you have a lot of variables to debug and therefore want something a bit easier. I found this code on another forum but I haven't had a chance to test it myself.

    
    //============ Function : print_debug( arr result of get_defined_vars,
    mixed variable to debug)
    
    function print_debug($defined_vars_array, $debug){
    
      $unset_unwanted =
    array("GLOBALS","_POST","_GET","_COOKIE","_FILES");
      foreach ($unset_unwanted as $remove){
        unset($defined_vars_array[$remove]);
      }
    
       while ($element = current($defined_vars_array)) {
       if ($element == $debug) {
    
           if (is_array($debug)){
             echo '$'.key($defined_vars_array).' = <br />'.
    print_r($debug, TRUE) . "<br />";
           } else {
             echo '$'.key($defined_vars_array).' = '. $debug . "<br />";
           }
       }
       next($defined_vars_array);
       }
    
    }
    
    //============= End of Function
    
    //=========================================
    // somescript.php
    
    // Test Variable
    $test_var1 = "This is a test Variable";
    $var_test = array(1,2,3);
    
    // debug the variable by calling "print_debug()"
    
    $arr = get_defined_vars();
    print_debug($arr,$var_test); 
    PHP:
    Source of code: http://groups.google.com/group/comp.lang.php/browse_thread/thread/e02037a5c14e54c9/3288eeef436b64f7

    Let me know if this works.
     
    Limotek, Sep 4, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    nico_swd, Sep 4, 2008 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Try this:
    <?php
    $test= ' something ';
    echo ' $test is '. $test;
    ?>
    The difference is in ' and " sign
    Same code, change the sign and see the output
    <?php
    $test= ' something ';
    echo " $test is ". $test;
    ?>

    First one will print the variable name, second will print it's value. :)
     
    JEET, Sep 4, 2008 IP
  5. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ah, I am actually looking *not* to use

    echo '$s is ', $s, "\n";

    but just p($s) and it will accomplish the task of printing out

    $s is Hello World

    it is possible in C and in Ruby... wonder if it is possible in PHP.
     
    winterheat, Sep 4, 2008 IP
  6. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    is it true that... what if $s is Hello World and $b is Hello World, then will it always print the right variable name? (or if $a is 0 and $i is 0)
     
    winterheat, Sep 4, 2008 IP