PHP Loop Dynamic Variable

php dynamic variable name
php variable in string
php variable variables array
php get variable name
php for loop
php
php variables types
php declare variable without value

I am trying to create a dynamic variable. I have a loop and I want it to loop through the records and create a variable for each record. My code:

$ct = 1;
foreach ($record as $rec){
  $var.$ct = $rec['Name'];
  $ct = $ct + 1;
}

echo $var1;

When I try to use the above code, it gives me an error saying the $var1 variable doesn't exist/undefined? Is it possible in PHP to create dynamic variables like the above example. If so, what am I doing wrong?

You're looking for variable variables.

Create the variable name as a string, and then assign it:

$ct = 1;
foreach( $record as $rec )
{
  $name = 'var'.$ct;
  $$name = $rec['Name'];
  $ct++;
}

echo $var1;

It would be much better to create an array, though:

$names = [ ];

foreach( $record as $rec )
{
  $names[] = $rec['Name'];
}

echo $names[0];

How do I dynamically create the variable name in a PHP loop , Look at the docs for "variable variables" - http://php.net/manual/en/language.​variables.variable.php <?php echo ${'standard_image_'.$step['number']}; ?>. Browse other questions tagged php loops variable-variables or ask your own question. The Overflow Blog The Loop, June 2020: Defining the Stack Community

You can use different syntax with {}

$ct = 1;

foreach ($record as $rec){
    ${'var' . $ct++} = $rec['Name'];
}

echo $var1;

Although isn't it better just to use an array?

Working fiddle

Is it possible to create variables dynamically in a for each loop?, I want to store data on items in my basket in session variables. The basket items are being displayed on the screen using the for each loop, and  PHP allows you to use dynamic variable names, called variable variables. You can name a variable with the value stored in another variable. That is, one variable contains the name of another variable. For example, suppose you want to construct a variable named $city with the value Los Angeles.

You can with a double $.

    $var = "variable";
    $$var = "test";

    echo $variable;
    //echoes "test"

in your example:

$ct = 1;
foreach ($record as $rec){
$varname = "var" . $ct;
  $$varname = $rec['Name'];
  $ct = $ct + 1;
}

echo $var1;

Dynamic Variable Names in PHP, A question I sometimes get asked is can you dynamically set the name of a variable in PHP. For example someone may have a loop, and they  Variable variables. Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as:

Please try this, let me know if it works for you. I use a prefix with the dynamic variable.

$ct = 1;
$prefix = 'var';
foreach ($record as $key=>$rec){
  $temp = $ct;
  $ct = $prefix.$ct;
  $$ct = $rec;
  $ct = $temp + 1;
}

echo $var1;

Variable variables - Manual, Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. Then this loop is . GET /api/widgetswhich returns a dynamic JSON list of widget IDs Loop over each widget ID, each time calling GET /api/widgets/and performing assertions on the return data. Since the IDs of the widgets are unknown to me (and always changing at that), it is impossible for me to statically define these in individual tests.

You can do that using array easily. But if you really want it to be in dyanamic vairable individually, in that case also , I would like to suggest you to get help in array way. This way, you can track you variables.

In the below mentioned way, you event don't need to take a extra variable like $ct. Just an array $var and applying extract method on it after the loop will do the trick.

$var = [];

foreach( $record as $k => $rec )
{
 $var['var'.$k] = $rec['Name'];

}

extract($var);

echo $var0; //or $var_1 or $var_2

php, I know there's a way that you can dynamically declare variable names, but I'm not exactly sure what the syntax looks like, so I'll let someone  Variables are the main way to store information in a PHP program. All variables in PHP start with a leading dollar sign like $variable_name. To assign a variable, use the = operator, with the name of the variable on the left and the expression to be evaluated on the right.

5.4. Creating a Dynamic Variable Name, Creating a Dynamic Variable Name Problem You want to construct a Use PHP's variable variable syntax by prepending a $ to a variable whose value is the title matches any of those values, the easiest way is to loop through them like this: init counter: Initialize the loop counter value; test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. increment counter: Increases the loop counter value; Examples. The example below displays the numbers from 0 to 10:

How do you create objects using a for loop., OK, if I understand you correctly, you would like to have variable names generated dynamically (i.e. at run time) rather than hard-coding them (such as obj1 , obj2 ,  Hi, I have 5 variables pos1 : 10 pos2 : 19 pos3 : 25 pos4 : 36 pos5 : 42 and i have a loop from 1 to 5 in which i have to change the variable name inside as so to access the content of the variable:

How to Use PHP Variable Variables, PHP allows you to use dynamic variable names, called variable variables. of variable variables becomes clear when they are used with arrays and loops. PHP Variables. A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for PHP variables: A variable starts with the $ sign, followed by the name of the variable; A variable name must start with a letter or the underscore character; A variable name cannot start with a number

Comments
  • What is $var??
  • My variable that im setting. The constant portion of the variable. It could be anything $name.$ct
  • Use arrays instead.
  • why are you not using array??
  • To be clear: It's not just that arrays are a much better solution to the problem, but also that using variable variables is virtually always the worst solution for anything.
  • I am getting undefined variable still when using your syntax. Have you tested this?
  • yes I have ;) let me include a fiddle just to prove sandbox.onlinephpfunctions.com/code/…