php make a variable with a variable - php

I'm not sure if this is possible, but I have a counter variable which increases by one every time.
Now I have a couple of variables above the counter which have a number after them e.g.
$var1=...
$var2=...
$var3=...
now inside the counter I would like the number after the variable to increase with the counter variable...
so like
for ( $counter = 1; $counter <= 3; $counter += 1) {
$var=$var$counter;
}
What I don't want is a string, I just want a number after a variable to change everytime the counter variable goes up by one.

This is called a variable variable. Any time you think about using one, realise that you should use an array instead.

Not sure what you mean by no string, but this should do it.
for ( $counter = 1; $counter <= 3; $counter += 1) {
$varName = 'var' . $counter;
$var=$$varName;
}
But I agree that a different method (array) should be used instead.

What are you trying to do with this?
I ask because it looks like you'd be better off using an array to achieve what you want. Then you could do something like:
for ( $counter = 1; $counter <= 3; $counter += 1) {
$var[$counter] = $value;
}

You'll want to change that code to this:
for ( $counter = 1; $counter <= 3; $counter += 1) {
${"var".$counter} = $var;
}
Wrapping the string in braces allows you to use it to dynamically create a variable name, so we simply concatenate "var" with the counter value, and then use it as the new variable name.
However, note that doing this will create and initialize all of the variables along the way... So in this example, you'll end up with $var, $var1, and $var2 all equal to the same value. If this is not what you want, you will have to unset() all of the variables you don't want.
Also note, however, that this is not a "best practice" in most cases. May I ask why you need to do this? Perhaps an array would be more suitable..

You can do that, yet it's not recommended.
It looks like this:
for ( $counter = 1; $counter <= 3; $counter += 1) {
$var = ${'var' . $counter};
}

Related

PHP array element as variable [duplicate]

how do i create variable variables inside a for loop?
this is the loop:
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
}
inside this loop i would like to create a variable $seat for each time it passes but it has to incrementlike so. first time it passes it should be $seat1 = $_POST['seat'+$aantalZitjesBestellen], next time it passes: $seat2 = $_POST['seat'+$aantalZitjesBestellen] and so on.
so at the end it should be:
$seat1 = $_POST['seat1'];
$seat2 = $_POST['seat2'];
and so on.
so the variable and the content of the $_POST should be dynamic.
Firstly, I would use an array for this unless I'm missing something. Having variables like $seat1, $seat2, etc tends to have far less utility and be far more cumbersome than using an array.
That being said, use this syntax:
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
$key = 'seat' . $counter;
$$key = $_POST[$key];
}
Lastly, PHP has an inbuilt function for extracting array keys into the symbol table: extract(). extract() has enormous potential security problems if you use it with unfiltered user input (eg $_POST) so use with caution.
This will work as well:
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
${'seat' . $counter} = $_POST['seat' . $counter];
}
(Expanded for clarity - you may be able to do a one-liner)
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
$varname = 'seat' . $counter;
$$varname = $POST[$varname];
}
BUT! You really shouldn't do this. (And if you really must, see cletus' answer for the built-in PHP way to do it - this is considered bad practice too, though.)
Reconsider your problem and see if arrays might be the solution (I guess it will). This will make both inspection (via e.g. var_dump()) and iteration easier and does not pollute the global variable space.
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
$name = 'seat' . $counter;
$$name = $_POST['seat' . $counter];
}
It's recommended to use arrays, as you can check them easier.
You can use extract but I don't recommended to do what you are trying to do.

How to refer to a variable dynamically [duplicate]

I have multiple PHP variables in the form
$number1, $number2, $number3 and so on...
I would like to dynamically reference these inside of a loop to retrieve information from them, but am not sure how to reference the static variable dynamically. Ex:
for($i = 1; $i <= 10; $i++) {
//The first number to be printed should be the value from
//$number1 not $number concatenated to $i
//here are some of the strings I tried:
echo "$number$i";
echo "{$number}$i";
echo "{$number}{$i}";
}
This should do it:
echo ${"number{$i}"};
But why not use arrays instead? Having $number[$i] is much more readable...

PHP dynamically reference variable in string literal

I have multiple PHP variables in the form
$number1, $number2, $number3 and so on...
I would like to dynamically reference these inside of a loop to retrieve information from them, but am not sure how to reference the static variable dynamically. Ex:
for($i = 1; $i <= 10; $i++) {
//The first number to be printed should be the value from
//$number1 not $number concatenated to $i
//here are some of the strings I tried:
echo "$number$i";
echo "{$number}$i";
echo "{$number}{$i}";
}
This should do it:
echo ${"number{$i}"};
But why not use arrays instead? Having $number[$i] is much more readable...

Can I add $i in variable to call the same sequence variable?

<?php
$listing_weblinkurl_1z1 = "some1";
$listing_weblinkurl_1z2 = "some2";
$listing_weblinkurl_1z3 = "some3";
$listing_weblinkurl_1z4 = "some4";
for($i=1;$i<=4;$i++){
print ($listing_weblinkurl_1z.$i);
}
?>
It is not working. I know that it is wrong to add $i in variable to call it. But I want it.
This is how it would be done, using variable variables.
for ( $i = 1; $i <= 4; $i+= 1 )
{
$varname = 'listing_weblinkurl_1z' . $i;
echo $$varname;
}
However, this is not a good way of writing code. Instead, $listing_weblinkurl should probably be an array containing keys of 1z1, 1z2, etc.
for($i=1;$i<=4;$i++) {
print ( ${"listing_weblinkurl_1z$i"} );
}
As #McAden pointed out in the comments, it would probably be a better idea to use an array.

variable variables

how do i create variable variables inside a for loop?
this is the loop:
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
}
inside this loop i would like to create a variable $seat for each time it passes but it has to incrementlike so. first time it passes it should be $seat1 = $_POST['seat'+$aantalZitjesBestellen], next time it passes: $seat2 = $_POST['seat'+$aantalZitjesBestellen] and so on.
so at the end it should be:
$seat1 = $_POST['seat1'];
$seat2 = $_POST['seat2'];
and so on.
so the variable and the content of the $_POST should be dynamic.
Firstly, I would use an array for this unless I'm missing something. Having variables like $seat1, $seat2, etc tends to have far less utility and be far more cumbersome than using an array.
That being said, use this syntax:
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
$key = 'seat' . $counter;
$$key = $_POST[$key];
}
Lastly, PHP has an inbuilt function for extracting array keys into the symbol table: extract(). extract() has enormous potential security problems if you use it with unfiltered user input (eg $_POST) so use with caution.
This will work as well:
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
${'seat' . $counter} = $_POST['seat' . $counter];
}
(Expanded for clarity - you may be able to do a one-liner)
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
$varname = 'seat' . $counter;
$$varname = $POST[$varname];
}
BUT! You really shouldn't do this. (And if you really must, see cletus' answer for the built-in PHP way to do it - this is considered bad practice too, though.)
Reconsider your problem and see if arrays might be the solution (I guess it will). This will make both inspection (via e.g. var_dump()) and iteration easier and does not pollute the global variable space.
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
$name = 'seat' . $counter;
$$name = $_POST['seat' . $counter];
}
It's recommended to use arrays, as you can check them easier.
You can use extract but I don't recommended to do what you are trying to do.

Categories