- array_search: documentation ( source)
<?php
function getVar(&$var) {
$tmp = $var; // store the variable value
$var = '_$_%&33xc$%^*7_r4'; // give the variable a new unique value
$name = array_search($var, $GLOBALS); // search $GLOBALS for that unique value and return the key(variable)
$var = $tmp; // restore the variable old value
return $name;
}
$city = "San Francisco";
$age = 55;
$object = new Stdclass;
$arr = array(3, 5, 6);
echo getVar($city). PHP_EOL;
echo getVar($age). PHP_EOL;
echo getVar($object). PHP_EOL;
echo getVar($arr). PHP_EOL;