Skip to content

Instantly share code, notes, and snippets.

@fntsrlike
Last active December 23, 2015 00:59
Show Gist options
  • Save fntsrlike/6557317 to your computer and use it in GitHub Desktop.
Save fntsrlike/6557317 to your computer and use it in GitHub Desktop.
This is a code snippet of PHP about how to get the variable name.
<?php
// ref: http://mach13.com/how-to-get-a-variable-name-as-a-string-in-php
$var = "hello, taiwan";
$name = var_name($var, get_defined_vars());
$val = $var;
echo $name; // var
echo $val; // hello, taiwan
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];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment