1

Some abstract code:

function test($a = 5) {
  debug_backtrace();
  a = 10;
}

What will debug_trace tell us about arguments of the test function?
Will it capture $a as 5 or 10?

mvlabat
  • 577
  • 4
  • 17

1 Answers1

0

If we call the function from the example this way:

test(4);

it'll capture '4'.

And if we call it this way:

test();

it'll capture actually no data about the arguments. I suppose parser doesn't initialize arguments if they haven't been used anywhere. (Calling debug_backtrace doesn't count.)

I've done some more researches and things have turned out a little bit unexpected (personally for me) if passing arguments by reference... But logical enough, I admit.

If we use the following code:

<?php
    function test2(&$a) {
            $a = 5;
            test($a);
            $a = 8;
    }
    function test(&$a) {
            $a = 6;
            print_r(debug_backtrace());
            $a = 7;
    }
    $test = 1;
    test2($test);

We'll get such output:

Array (
  [0] => Array (
    [file] => /var/www/localhost/htdocs/index.php
    [line] => 4
    [function] => test
    [args] => Array ( [0] => 6 ) 
  )
  [1] => Array (
    [file] => /var/www/localhost/htdocs/index.php
    [line] => 13
    [function] => test2
    [args] => Array ( [0] => 6 ) 
  ) 
)

So debug_backtrace() always prints current state of function arguments passed by references (when debug_backtrace() was actually called), no matter if they had another value on a parent function call.
Be careful when debugging! :)

mvlabat
  • 577
  • 4
  • 17