tags:

views:

156

answers:

6

func($name1) should return name1

Is it possible?

+5  A: 

No.

Chacha102
Doesn't SO have validation for replies that are too short?
AK
But it isn't too short. How would I have posted it if it was too short? :P
Chacha102
ahahaha, there's a lot of whitespaces.... can't see it?i think SO needs to trim comments and answers because of this....
ultrajohn
` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `
Chacha102
am wrong... the whitespaces actually get trimmed..
ultrajohn
16 year olds...
AK
+4  A: 

No, there is no way to get the name of a variable in PHP.

When calling a function, that function will only receive the content of the variable, and not the "variable itself" -- which means a function cannot find out the name of the variable that was passed to it.

Pascal MARTIN
I liked my answer better....
Chacha102
My answer gives a bit more informations :-p
Pascal MARTIN
Chacha102
But giving a couple of explanations *(i.e. some words about "why" this is not possible)* is nice too, don't you think ?
Pascal MARTIN
I'll give you that.
Chacha102
You two should stop arguing and notice that someone else, andyjdavis, posted a *helpful* answer.
bmb
@bmb ... If you require the need to access the name of a variable, you have a design problem...
Chacha102
@Chacha102, maybe so, but that doesn't mean it's not possible.
bmb
+2  A: 

No.

When you define a function, you specify a local variable name for it to have inside the scope of that function. PHP will pass the function the appropriate value, but the symbol is no longer in scope.

You could look into using "variable variables" as an alternative, however.

Mike Trpcic
+5  A: 

Here's a function that does it.

function var_name (&$iVar, &$aDefinedVars)
{
    foreach ($aDefinedVars as $k=>$v)
        $aDefinedVars_0[$k] = $v;

    $iVarSave = $iVar;
    $iVar     =!$iVar;

    $aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
    $iVar      = $iVarSave;

    return $aDiffKeys[0];
}

Call it like this

$test = "blah";
echo var_name($test,  get_defined_vars());

That will print out "test".

I originally found that function over here You can also do it by iterating over the array returned by get_defined_vars(). That might be a bit easier to understand.

andyjdavis
I tried this in CodePad (http://codepad.org/w11zjYUv) and it worked. People should vote this answer up. I wonder why the other answer with a simple "No" got way more up votes than this one...
Kai Chan
People should think of this code necessity.Think of array use if you need a key's nameBut leave variable abstract.
Col. Shrapnel
"No" has more votes because it takes less time to type "no" than it does to test and describe a solution. Others come in after "no" is posted but before the answer is and vote up "no" because they don't know how and so assume it is therefore impossible. It'll work itself out hopefully :)
andyjdavis
This involves copying the value of every variable in scope, which is pretty inefficient. It should work, but it's not something I'd want to use.
Wyzard
And people are downvoting *this* answer? What's up with that?
bmb
+1  A: 

Obviously, it is possible for sufficiently high values of crazy.

The comments on this page include several techniques: http://php.net/manual/en/language.variables.php

lucas dot karisny at linuxmail dot org's answer works on my machine: http://www.php.net/manual/en/language.variables.php#49997

YMMV.

Jivlain
+1  A: 

Good Idea ? No

Any usecase you where you should do it ? No

Proof of concept ? Sure !

<?php

a($test);


function a($x) {
    $trace = debug_backtrace();
    $file = file($trace[0]['file']);
    $line = $file[$trace[0]['line']-1];

    var_dump($line); // Prints "a($test);" Do the Stringparsing and your done

}

Yes, this takes the "easy" by reading the sourcefile, it is also doable by using a php extension called "bytekit" that gives you userland access to the php opcodes and work from there.

edorian