original variable name passed to function? [duplicate]

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.


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();

Surely, this is not possible without creating a PHP extension for it.

You certainly don't want to rely on that, especially in a function. A function should not be able to operate outside its own scope (except for globals, but I hate them).