How can I find out the name of a variable inside a function?

rubo77

I have a function

function my_dump($a,$name){
    echo '<pre>'.$name.":\n";
    var_export($a);
    echo '</pre>';
}

How can I achieve this with only one argument so the function recognizes the name of the called variable itself?

for example:

my_dump($cool_variable_name);

will output:

Name: "cool_variable_name"
Content: array(...

Václav
function my_dump($a) {
$backtrace = debug_backtrace()[0];
$fh = fopen($backtrace['file'], 'r');
$line = 0;
while (++$line <= $backtrace['line']) {
    $code = fgets($fh);
}
fclose($fh);
preg_match('/' . __FUNCTION__ . '\s*\((.*)\)\s*;/u', $code, $name);
echo '<pre>'.trim($name[1]).":\n";
var_export($a);
echo '</pre>';
}

It does not run right - because used expression does not give correct result.

Using of \s* has not sense because nothing can be between name of function and brackets with parameters.

Using of ; in expression has not sense too. At least because is allows only standalone using of this function where result of this function is given to else variable. But sometimes it may be needed to use this function as parameter.

And using of (.*) will allow also all that is behind variable name given as parameter - even if there is semicolon.

So, it is needed to exclude of sign of end bracket, ), from result. There are many ways how to do that.

You can use form from code of function below or this (or something else)

'/'.__FUNCTION__.'\((\w+)\)/'

Whole function may be very simplified into (for example, this is case of class function):

protected function Get_VariableNameAsText($Variable="")
{
    $File = file(debug_backtrace()[0]['file']);

    for($Line = 1; $Line < count($File); $Line++)
    {
        if($Line == debug_backtrace()[0]['line']-1)
        {
            preg_match('/'.__FUNCTION__.'\(([^\)]*)\)/', $File[$Line], $VariableName);
            return trim($VariableName[1]);
        }
    }
}

I deleted using of var_export because I don't see why to use it, when I only want to get variable name as string (text). And printing of given string is better outside of this function.

If you have not 5.4 or greater, you need to change code

$File = file(debug_backtrace()[0]['file']);
if($Line == debug_backtrace()[0]['line']-1)

on

$Trace = debug_backtrace();
$File = file($Trace[0]['file']);
if($Line == $Trace[0]['line']-1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I find out the name of a variable inside a function?

From Dev

How can I include a variable inside a callback function?

From Dev

How can I get the value from this variable inside function?

From Dev

How can I find object type and name of the executing function?

From Dev

How can I find object type and name of the executing function?

From Dev

Scala: How can I get the name of a variable class in a function?

From Dev

python: how can I pass a variable to a function with only the string name

From Dev

How can I find out what app is running (actual name, not published name)?

From Dev

Why can’t I assign values to a variable inside a named function expression with the same name?

From Dev

How can I print out the complete function declaration of any function which has a specific string inside it?

From Dev

I can't figure out How to call previously created function inside another function?

From Dev

I can't find the function name

From Dev

How can I find out a variables' name size in memory in C++?

From Dev

How can I print out only the name and file size using the find command?

From Dev

How can I find out the name of the user when in a ASP.Net Web API Controller?

From Dev

How can I find out the name of a Windows PC on my LAN by MAC address?

From Dev

How can I call a function inside of a function?

From Dev

How can I find out about "rootfs"

From Dev

How can I find the docker name from a program running inside Docker

From Dev

how can I change values inside a variable?

From Dev

How can I access a variable inside an if statement

From Dev

How can I find out what linker flags are needed to use a given C library function?

From Dev

How can I find out what linker flags are needed to use a given C library function?

From Dev

How can I find out what calls the push-mark function in emacs?

From Dev

How can I find out the number of arguments that are actually passed to a bash function?

From Dev

Is there a variable i can query to find out if a feature was selected for installation in the FeaturesDlg

From Dev

How can I abstract out this .each function?

From Dev

How can I find out which index is out of range?

From Dev

How can I find a button by name in designer?

Related Related

  1. 1

    How can I find out the name of a variable inside a function?

  2. 2

    How can I include a variable inside a callback function?

  3. 3

    How can I get the value from this variable inside function?

  4. 4

    How can I find object type and name of the executing function?

  5. 5

    How can I find object type and name of the executing function?

  6. 6

    Scala: How can I get the name of a variable class in a function?

  7. 7

    python: how can I pass a variable to a function with only the string name

  8. 8

    How can I find out what app is running (actual name, not published name)?

  9. 9

    Why can’t I assign values to a variable inside a named function expression with the same name?

  10. 10

    How can I print out the complete function declaration of any function which has a specific string inside it?

  11. 11

    I can't figure out How to call previously created function inside another function?

  12. 12

    I can't find the function name

  13. 13

    How can I find out a variables' name size in memory in C++?

  14. 14

    How can I print out only the name and file size using the find command?

  15. 15

    How can I find out the name of the user when in a ASP.Net Web API Controller?

  16. 16

    How can I find out the name of a Windows PC on my LAN by MAC address?

  17. 17

    How can I call a function inside of a function?

  18. 18

    How can I find out about "rootfs"

  19. 19

    How can I find the docker name from a program running inside Docker

  20. 20

    how can I change values inside a variable?

  21. 21

    How can I access a variable inside an if statement

  22. 22

    How can I find out what linker flags are needed to use a given C library function?

  23. 23

    How can I find out what linker flags are needed to use a given C library function?

  24. 24

    How can I find out what calls the push-mark function in emacs?

  25. 25

    How can I find out the number of arguments that are actually passed to a bash function?

  26. 26

    Is there a variable i can query to find out if a feature was selected for installation in the FeaturesDlg

  27. 27

    How can I abstract out this .each function?

  28. 28

    How can I find out which index is out of range?

  29. 29

    How can I find a button by name in designer?

HotTag

Archive