variable variables - php

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.

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.

Foreach into PHP array

I've this these array values :
$cart_item['addons'][0]['price'] = '52';
$cart_item['addons'][1]['price'] = '34';
$cart_item['addons'][2]['price'] = '12';
......
....
I want that each values are at 0 like :
$cart_item['addons'][0]['price'] = '0';
$cart_item['addons'][1]['price'] = '0';
$cart_item['addons'][2]['price'] = '0';
....
...
So I try this code :
for ($i=0; $i > 0 ; $i++) {
$cart_item['addons'][$i]['price'] = '0';
}
But it does not work. Thanks for your help !
Try this simple solution:
$count=count($cart_item['addons']);
for($i=0; $i<$count;$i++ ){
$cart_item['addons'][$i]['price'] = '0';
}
If your array is big enough, putting count() function inside for loop is being a crazy coconut. It will be much, much slower. Please use the count outside the loop:
$count = count($cart_item['addons'])
for($i=0; $i<$count;$i++ ){
$cart_item['addons'][$i]['price'] = '0';
}
You have to loop more often to achive this:
foreach($cart_item['addons'] as &$addons {
foreach($addons as &$addon) {
$addon['price'] = 0;
}
}
You can iterate over $cart_item['addons'], like so:
foreach ($cart_item['addons'] AS $key => &$value) {
$value['price'] = 0;
}
(Your code does not get executed, because $i is never changed and so is never > 0)
Also note that you need to use the reference (&$value) when changing the array in foreach loop.
There are three parts to your for loop: for(counter | test | action){}. It might be useful for you to look at this guide about for loops. You initialise your variable:
$i = 0;
then you do a logical check (the test part):
$i > 0;
If we substitute the the variable ($i) for the value it holds (0) we get:
0 > 0
which will never be true and thus you will never get to the final part (action) of the for loop:
$i++;
Instead you could make it loop until it has moved through your entire array like this:
$elementCount = count(cart_item['addons']);
for($i=0; $i < $elementCount; $i++){
$cart_item['addons'][$i]['price'] = '0';
}
Each time it loops we add one to $i until we reach the stopping condition where $i is no longer less than the number of items in the array.
It's also worth noting that PHP has a number of functions that help working with arrays.

PHP - for loops through associative array

I have an associative array $_POST, which has 3 key value pairs (There are other key value pairs which I am not interested in).
$_POST[Var1]
$_POST[Var2]
$_POST[Var3]
How do I use a for loop to loop through and echo the values in each?
for ($i = 1; $i <= 3; $i++){
echo $_POST['Var' . '$i'];
}
This does not seem to work.
Get rid of the single quotes around $i as that makes it a literal string and your variable is not interpolated:
for ($i = 1; $i <= 3; $i++){
echo $_POST['Var' . $i];
}
This is basic PHP. I strongly recommend reading the manual to learn more about the fundamentals of PHP.
Check this. While using php varibles dont put upper commas for the variables.
<?php
$_POST["file1"];
$_POST["file2"];
$_POST["file3"];
for ($i = 1; $i <= 3; $i++){
echo $_POST['file'.$i];
}
Or this way:
$_POST["Var{$i}"];

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.

php make a variable with a variable

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};
}

Categories