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...
Related
i want to create a for loop in order to unset the spesific variables in an array. i cant find any answer on the internet. here is my code.
$randomnumber=242;
$variables= array('var','var2','randomnumber');
for ($i = 0; $i < count($variables); $i++) {
unset($variables[$i]);
}
echo $randomnumber;
output is:
242
i dont know what am i missing. please help me guys. i want to unset "var1", "var2", and "randomnumber" variables in the array of "variables". output should be "undefined variable : $randomnumber" or smth like that.
Code
unset($variables[$i]);
means
unset value with key $i from array $variables
If you want to unset a variable with name $variables[$i] then you should use variable variable:
$randomnumber=242;
$variables= array('var','var2','randomnumber');
for ($i = 0; $i < count($variables); $i++) {
// variable variable syntax here
unset(${$variables[$i]});
}
echo $randomnumber;
simpler and faster solution:
$randomnumber = 242;
$variables = ['var', 'var2', 'randomnumber'];
foreach ($variables as $variableName) {
unset($$variableName);
}
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...
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}"];
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'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};
}