1. Finding the variable name by value
<?php
function var_name(&$var, $scope=0)
{
$key = array_search($var, $GLOBALS);
return $key;
}
$name = 'samy';
echo var_name($name);
?>
O/P
name
Above code will works for only unique variable used for the page. finding the variable name by value with multiple variable name in a same page use following function
<?php
echo vname($inp);
function vname(&$var, $scope=false, $prefix='unique', $suffix='value')
{
if($scope) $vals = $scope;
else $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val) {
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}
?>
O/P
name
<?php
function var_name(&$var, $scope=0)
{
$key = array_search($var, $GLOBALS);
return $key;
}
$name = 'samy';
echo var_name($name);
?>
O/P
name
Above code will works for only unique variable used for the page. finding the variable name by value with multiple variable name in a same page use following function
<?php
echo vname($inp);
function vname(&$var, $scope=false, $prefix='unique', $suffix='value')
{
if($scope) $vals = $scope;
else $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val) {
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}
?>
O/P
name
No comments:
Post a Comment