imagine that I have 3 variables in a array:
$var1 = "a";
$var2 = "b";
$var3 = "c";
$a = array($var1, $var2, $var3);
foreach($a as $var)
{
//code that create strings with variable names and echo var1 , var2 , var3
}
Is this posible to do that with php?
If you’re looking to see if you can recall the variable names from the array that you created, I’m sorry to tell you this but it’s not going to be possible (at least, in the way your example has it). When you’re creating that array, you’re making a copy of those variables and storing them in the array. You aren’t actually pointing to the original variables.
Couple of options of the top of my head.
Option 1: Create the array with the variable names as the keys.
Option 2: Assign the variable’s name to the variable itself beforehand. Ex:
$var1->VAR_NAME = 'var1';Option 3: Try a workaround like the one suggested here: How to get a variable name as a string in PHP?
Option 4: If you just need it to reference the original variable, try capturing the reference to the object itself using
&.Hope that helps.