How to Get a Variable Name as a String in PHP

How to get a variable name as a string in PHP?

You could use get_defined_vars() to find the name of a variable that has the same value as the one you're trying to find the name of. Obviously this will not always work, since different variables often have the same values, but it's the only way I can think of to do this.

Edit: get_defined_vars() doesn't seem to be working correctly, it returns 'var' because $var is used in the function itself. $GLOBALS seems to work so I've changed it to that.

function print_var_name($var) {
foreach($GLOBALS as $var_name => $value) {
if ($value === $var) {
return $var_name;
}
}

return false;
}

Edit: to be clear, there is no good way to do this in PHP, which is probably because you shouldn't have to do it. There are probably better ways of doing what you're trying to do.

creating variable name by concatenating strings in php

Variable variable is not recommended, but the answer is below:

$question = ${'question'.$var}[0];

Getting PHP variable name as string for many variables

I'm not sure if this is what your after, but in the example below, I've just created a foreach() loop to check if the array value from $ints[] is true. If so, then add variable name and value to $varArr array. So the new array would look like ["fa1" => "DESKTOP", "fa2" => "VOIP"]. You then loop through this new array and compare the value using a Switch. Note that $fa4 has been ignored on purpose but you can easily add this in if needs be and test using a default case.

$fa1 = "DESKTOP";
$fa2 = "VOIP";
$fa3 = "DESKTOP";
$fa4 = "";
$fa5 = "DESKTOP";

$ints = [$fa1,$fa2,$fa3,$fa4,$fa5];
$varArr; //Variable Name Array

foreach($GLOBALS as $k=>$v) { //Looping through Globals

if(in_array($v, $ints) && $v !== "") { //If value is in $ints array, add to this array

$varArr[$k] = $v;
}
}

foreach ($varArr as $k=>$v) { //Create Interface File

file_put_contents($file, "interface ". $k ."\n", FILE_APPEND);

switch($v) {
case "DESKTOP":
file_put_contents($file, "standard desktop config\n", FILE_APPEND);
break;
case "VOIP":
file_put_contents($file, "standard voip config\n", FILE_APPEND);
break;
}
}

Output:

interface fa1
standard desktop config
interface fa2
standard voip config
interface fa3
standard desktop config
interface fa5
standard desktop config

how can i print var name instead var value in PHP?

I think what you want is this:

$array = array('Tree' => 'abcd', 'str' => 'zwd');
foreach ($array as $name => $value) {
echo $name . ' = ' . $value;
}

Can you get a variable name as a string in PHP? (And should you?)

There are multiple problems with what you want to do. For example, the name of the variable is not accessible. The code $arr = array($foo, $bar, $baz) creates an array with the values of the $foo, etc. If you then changed the value of $foo, the value of $arr[0] is still the old value of $foo, so it's not even clear what it would mean to have access to the variables name.

Even if it were easy to do, I would consider it very poor practise, simply because you'd need to know by introspection what the correct variable names would be.

Of course, this is all easily solved if you had an associative array. For example:


$arr = array('Foo' => $foo, 'Bar' => $bar, 'Baz' => $baz)

This could easily be altered to produce what you want. For example:


$display = array();
foreach($arr as $key => $value)
$display[$key] = $prefix . $value . $suffix;

Is there a way to get the name of a variable? PHP - Reflection

You can do it by converting the variable to a key/value set before passing it to the function.

function varName($theVar) {  
$variableName = key($theVar);
$variableValue = $theVar[$variableName];
echo ('The name of the variable used in the function call was '.$variableName.'<br />');
echo ('The value of the variable used in the function call was '.$variableValue.'<br />');
}
$myVar = 'abc';
varName(compact('myVar'));

Though I don't recommend creating a reference to a nameless variable, function varName(&$theVar) works too.

Since compact() takes the variable name as a string rather than the actual variable, iterating over a list of variable names should be easy.

As to why you would want to do this -- don't ask me but it seems like a lot of people ask the question so here's my solution.

PHP put the variable name in a string

PHP offers no simple way of doing this. The PHP developers never saw any reason why this should be neccesary.

You can however use a couple of workarounds found here:
Is there a way to get the name of a variable? PHP - Reflection and here: How to get a variable name as a string in PHP?

PHP- concatenate string with iterator to get original variable name

Use it this way.

Try this code snippet here

<?php
$encoded1=10;
$encoded2=20;
$encoded3=30;

for($i =1;$i<=3;$i++)
{
echo ${"encoded".$i};
}

PHP, how to use a character as a variable name?

You should refer to PHP Variable variables.

A variable variable takes the value of a variable and treats that as the name of a variable.

Try one of these:

$string = "ABCD";

echo ${$string[0]}; // 10
echo $$string[0]; // 10


Related Topics



Leave a reply



Submit