<?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.
Related
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.
Is it possible to use a for loop where $i is part of the variable name? I'm trying to get this for loop to list the fruits:
$item1name = "apple";
$item2name = "orange";
$item3name = "banana";
for($i=0, $i<2, $i++) {
echo = "<li>$item?????</li>";
}
// should result in:
// <li>apple</li><li>orange</li><li>banana</li>
I realize I can put the fruits in an $itemname array and easily echo $itemname[$i], but that isn't what I'm asking. Is it possible to do this when $i is part of the variable name?
It is possible with the use of variable variables:
for($i=1; $i<=3; $i++) {
echo "<li>" . ${'item'.$i.'name'} . "</li>";
}
Note that your original code wasn't syntactically correct. I've fixed it. Also note how $i value changed. Your variable numbers are from 1 to 3, not 0 to 1.
But I don't see why you'd want this. Simply use an array instead.
Demo
I know what i have implemented here is wrong i want it to do it correctly that is why asking help here.Don't know whether this is possible or not.
<?php
$test1="hello";
$test2="how";
$test3="are";
for($i=1;$i<=3;$i++)
{
echo $test.$i;
}
?>
When i run this i should get hello how are .i know string concatenation same thing i want to do it for variable also.Is this Possible, if possible by this i can easily access all those variable. Any help?
Try with following syntax:
echo ${'test'.$i};
I guess what you're looking for is the "array". You can use arrays in PHP like this:
$test = array('hello', 'how', 'are');
$len = count($test);
for($i=0; $i<$len; $i++) {
echo $test[$i];
}
Try this:
for($i = 1; $i < 4; $i++){
echo $test{$i};
}
I am trying this code:
for ($x = 0; $x < $numCol; $x++) {
for ($i = 0; $i < $numRows; $i++) {
$arr.$x[] = $todas[$i][$x]."\n"; //problem here
}
}
echo $arr0[0];
echo $arr1[0];
...
But i get this warning: Cannot use a scalar value as an array
and the echos do nothing. Why ? and what is the solution ?
Here's what you think you want to do. Replace your //problem here line with:
${'arr' . $x}[] = $todas[$x][$i]."\n";
But I would strongly recommend against doing that. Just use your bidimensional array.
I think you meant: ${'arr'.$x}[] instead of $arr.$x[].
$arr.$x[]
Will concatenate the string representation of $arr and $x together so you end up with something like 'Array0'[] = ... instead of $arr0[]
When you write $arr.$x[], it is equal to $arr[$x][]
Try replacing your echos by
echo $arr[0][0];
echo $arr[1][0];
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.