Method 1: Using $GLOBALS
Example: This example uses $GLOBAL reference to get the variable name as a string.
<?php
// Declare and initialize a variable php
$test="this is string";
// Function that returns the variable name
function getVariavleName($var){
foreach($GLOBALS as $varName=>$value){
if($value==$var){
return $varName;
}
}
return;
}
// Function call and display the
// variable name
print getVariavleName($test);
?>
<?php
// Declare and initialize a variable
$name="text";
// Reference variable to store string
$name="this is a string";
// Display result
print($name);
?>