Less typing / more debugging

Discussion in 'PHP' started by Stumped, Apr 22, 2010.

  1. #1
    Like many of you, as I write and test web page php code I need to frequently and temporarily display many values on the browser screen. Because I do this so often I want to keep the typing to an absolute minimum. To show the variable $x=123, echo $x; is the least typing, but only the value appears, and if I'm displaying several values it can get confusing as to which value is which. var_dump($x) is good, but sometimes long winded and echo "x: $x<br />"; is better, but more typing.

    I'm working on a single letter utility/helper function "d" used as follows:

    d('x');

    displays:

    x: 123

    Here is the function:

    Function d($varname) {global $debug, ${$varname}; if ($debug) echo " $name: ${$varname}<br />";}

    While this works great with simple variables like $x, I can't seem to get it to work for array values like $x[1].

    Can any of you rewrite function d() above to make it work for both array variables as well simple variables, or failing that make a new function a() to do the same as d() but for array variables?

    -----------------------------------------------
    I admit this may not be possible. I've tried everything I can think of including using eval(). (I also admit that I was spoiled years ago with Forth where I could write any new language construct bar none in Forth for Forth. While I love php, it is sort of frustrating to not be able to build small development tools for php to assist with faster code creation.)
     
    Stumped, Apr 22, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Unrelated but;
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Why are you using a global variable here? Thats incredibly bad practice - as is eval();

    You say this is for a helper function - if you mean to release to others i would like to think the take up would be minimal, the amount of code involved is tiny so i think you are wasting your time; its why most of it is built in already. (and if you are serious, look up writing your own error handlers which you can feed debug info rather than trying to do it shorthand).
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    That said;

    print_r($var) is about as good as you are going to get - have you tried that yet? By the time you have parsed the array, it could already have been done with that.
     
    lukeg32, Apr 22, 2010 IP
  3. Stumped

    Stumped Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I've written about 20k finished lines for my current php project. (Probably we're looking at 100k lines of typing when all of the revisions are taken into account.) If I can cut my typing down even a bit it helps.

    print_r() has four extra letter to type over 'echo ', i.e. '_r()'. So your suggestion is 45% worse, not better.

    May I suggest that eval is a perfectly good function when used appropriately. In fact there are times that only eval will do the job, that's why it exists.

    As far as using global, that too is a perfectly good way to do things when it best serves the need.
     
    Stumped, Apr 22, 2010 IP
  4. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #4
    *sigh* From the creator of PHP: "If eval() is the answer you are asking the wrong question"......

    LOL .... right... amateur coder, my talent here is wasted then, good luck getting your answer.

    With eval and globals remind me to stick well clear of anything you do.
     
    lukeg32, Apr 22, 2010 IP