Wednesday 27 December 2017

php - original variable name passed to function?

You can not get the variable name, but if you want it for
debugging, then you can use PHP's built-in href="http://php.net/manual/en/function.debug-backtrace.php"
rel="nofollow">debug_backtrace(), and I recommend
to take a look on Xdebug
as well.



With this function, you can get some
data on the caller, including the file and line number, so you can look up that line
manualy after running the
script:



function
debug_caller_data()
{
$backtrace =
debug_backtrace();


if (count($backtrace) >
2)
return $backtrace[count($backtrace)-2];
elseif
(count($backtrace) > 1)
return $backtrace[count($backtrace)-1];

else
return
false;
}


Full
example:





function
debug_caller_data()
{
$backtrace =
debug_backtrace();

if (count($backtrace) > 2)
return
$backtrace[count($backtrace)-2];
elseif (count($backtrace) >
1)

return $backtrace[count($backtrace)-1];

else
return false;
}

function
write($var)
{

var_dump(debug_caller_data());
}



function
caller_function()
{
$abc = '123';

write($abc);
}

$abc =
'123';
write($abc);


caller_function();

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...