15

Possible Duplicates:
PHP: Get name of variable passed as argument
How to get a variable name as a string in PHP?

If I have

$abc = '123';

function write($var){

}

How would I inside write find out that the variable now represented by $var was called $abc?

Community
  • 1
  • 1
Hailwood
  • 89,623
  • 107
  • 270
  • 423
  • 4
    Even if it was really represented in your code, there is no way anyway. forget it. – Your Common Sense Dec 21 '10 at 20:12
  • 1
    You can't. Only the value is passed to the function. – Pekka Dec 21 '10 at 20:12
  • 3
    Why do you want to do this? Chances are that there is an easier or more appropriate method (or you just shouldn't do it to begin with)... – ircmaxell Dec 21 '10 at 20:13
  • @Gordon: interestingly, that question too was closed as a duplicate :) – Jonah Dec 21 '10 at 20:14
  • 2
    possible duplicate of [How to get a variable name as a string in PHP?](http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php) (the one that was duplicated in @Gordon's post) – ircmaxell Dec 21 '10 at 20:16
  • 1
    @Jonah that's because ppl are too lazy to search nowadays. I am sure there is a number of additional duplicates – Gordon Dec 21 '10 at 20:17
  • This is simply for some debug functions I am writing. They will never be used in production. – Hailwood Dec 21 '10 at 20:34
  • @Gordon: That's the reason, not the cause -- which is avoiding the fusty SO search function. – mario Dec 21 '10 at 20:48
  • @mario I agree the search function could be improved and for some questions it can be frustrating or even impossible (e.g. searching for operators). But finding the dup for this one took less than a minute. – Gordon Dec 21 '10 at 22:46
  • 1
    I admit, I did not use the SO search, although I did use google search, normally if there is a duplicate it will show up. apologies. – Hailwood Dec 21 '10 at 23:17
  • Everybody is saying this is impossible, but it's not. The very nice php debugging library 'Kint' https://github.com/kint-php/kint does this with everything you pass into it. I've stepped through the code, and it looks like what it's basically doing is getting a debug_backtrace, then parsing it out to get info about the code that called it, including what was passed into the function (variable name, string, expression, etc), and where it was called from. – Aaron Wallentine May 10 '17 at 21:44
  • This should be un-marked as duplicate-- the [first listed duplicate](http://stackoverflow.com/questions/2379166) , is basically empty, and is marked as dup of this, so neither can be edited. This one has more answers, and should therefore be canonical, and unmarked so that further answers can be given. [The second one](http://stackoverflow.com/questions/255312) is not a duplicate, because it only refers to getting a var name, not a var name passed into a function. It's a different question. @Gordon or someone, please unmark this. – Aaron Wallentine May 10 '17 at 22:00

2 Answers2

11

It's not possible. Even pass-by-reference won't help you. You'll have to pass the name as a second argument.

But what you have asked is most assuredly not a good solution to your problem.

Jonah
  • 9,991
  • 5
  • 45
  • 79
3

You can not get the variable name, but if you want it for debugging, then you can use PHP's built-in 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:

<?php

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();
István Ujj-Mészáros
  • 3,228
  • 1
  • 27
  • 46
  • I tested your **full example**. Both invoking of `write` method, have the same output; except about `line` field (that has a value equal to the line of invoking in each case). – Mir-Ismaili Oct 03 '17 at 16:57
  • Do you know about the performance implication ? Is that information something we get 'for free' because it's readily available or does it cause a performance hit ? – John Oct 25 '19 at 16:18
  • I don't know if it has any performance impact, but you can just profile your code to see if it has. – István Ujj-Mészáros May 13 '20 at 04:10