Php Get Variable Name As A String

[Solved] Php Get Variable Name As A String | Php - Code Explorer | yomemimo.com
Question : php get variable by string name

Answered by : yoshkinawa

${$variableName} or $$variableName;
//example:
$variableName = 'foo';
$foo = 'bar';
// The following are all equivalent, and all output "bar":
echo $foo;
echo ${$variableName};
echo $$variableName;

Source : | Last Update : Mon, 11 Jan 21

Question : php get variable name as a string

Answered by : kind-koala-kdcmuj7v52n3

function print_var_name(){ // read backtrace $bt = debug_backtrace(); // read file $file = file($bt[0]['file']); // select exact print_var_name($varname) line $src = $file[$bt[0]['line']-1]; // search pattern $pat = '#(.*)'.__FUNCTION__.' *?\( *?(.*) *?\)(.*)#i'; // extract $varname from match no 2 $var = preg_replace($pat, '$2', $src); // print to browser echo trim($var);
}

Source : https://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php | Last Update : Tue, 11 May 21

Answers related to php get variable name as a string

Code Explorer Popular Question For Php