Last active
December 23, 2015 00:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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