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 use the array_rand PHP function with an array. I use it with a data fixture function wich load a set of data in a loop like this:
$random_values = array();
for ($i = 0; $i < 20; $i++) {
$random_values[] = array_rand(["1","2","3","4","5"]);
}
My result is quite always "1" in the $random_values array, the native
PHP function seems not really random, Is there another stuff to do to
improve the randomization of my algorithm ?
Notice I already know there is an official documentation here, http://php.net/manual/fr/function.array-rand.php.
How's it going? So, with array_rand, it actually returns a random key within an array. Your current code does not put the random key value into the array you want to randomize. ie echo $random_value[$random_key]... See example below, hope this helps
$random_values = array("1","2","3","4","5");
for ($i = 0; $i < 20; $i++) {
$key = array_rand($random_values);
echo $random_values[$key] . "\n";
}
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 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 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...