Afficher un message
Vieux 12/10/2007, 22h31   #2
Janwillem Borleffs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting names of variables passed to functions...

BoneIdol wrote:
> I'd like the function foo to return a string of the variable name
> passed to it, in this case 'variable_name'. A friend of mine who does
> C ++ programming says that pointers are the way to go here, but as far
> as I know PHP doesn't support them.
>


A (not fullproof) way is the following:

function foo($bar) {
$keys = array_keys($GLOBALS);
$values = array_values($GLOBALS);
$index = array_search($bar, $values, true);
if ($index !== false) {
return $keys[$index];
}
}

$variable_name = 'foo';
$name = foo($variable_name);
print $name;

Of course, this only works when each variable has a unique value...


JW


  Réponse avec citation
 
Page generated in 0,05145 seconds with 9 queries