I need to create a php file with a hundred variables, which are all identical except for their id.
PHP Code
$myvar1 = get_input('myvar1');
$myvar2 = get_input('myvar2');
$myvar3 = get_input('myvar3');
$myvar4 = get_input('myvar4');
...
$myvar100 = get_input('myvar100');
I wonder if it is possible to create only one line as a model, and is replicated 100 times?
Thanks.
Just store it in an array instead of 100 variables:
$myvar = Array();
for ($i = 1; $i <= 100; ++$i) {
$myvar[$i] = get_input('myvar' . $i);
}
Or if you want the indexes to start at zero:
$myvar = Array();
for ($i = 1; $i <= 100; ++$i) {
$myvar[$i - 1] = get_input('myvar' . $i);
}
You really must use Arrays for this, its far more appropriate:
$myvar = Array();
for($i=1;$i<101;$i++) $myvar[$i]=get_input("myvar{$i}"); // $myvar[1]=... etc...
Anyway, its possible to create variable names dynamically (notice the '$$'):
for($i=1;$i<101;$i++){
$varName="myvar{$i}";
$$varName=get_input($varName);
}
you could store your variables in an array
$myvarArr = array();
for($i=0;i<100;i++) {
$myvarArr[$i] = get_input('myvar' . ($i+1));
}
Looks like you just need an array, an array right from the outside variable.
if you make your form field name like this
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
you can easily get all the values by as simple code as
$myvar = $_POST['myvar'];
Just to show, that it's possible, use variable variables (i just love this php feature):
for ($i = 1; $i <= 100; $i++) {
$varName = "myvar{$i}"; // create variable name
$$varName = get_input("myvar{$i}"); // $$varName => $myvar5, when $i == 5 and so on
}
Related
I am trying to make this code small using for or while loop . It is working with the following code,I just want this to be small. 'ufile' is the input name.
if (!$_FILES['ufile']['name'][0])
{
echo "Upload 1st file";
}
else
{
// can this be in a for loop???
$path1= "../uploads/".$_FILES['ufile']['name'][0];
$path2= "../uploads/".$_FILES['ufile']['name'][1];
$path3= "../uploads/".$_FILES['ufile']['name'][2];
$path4= "../uploads/".$_FILES['ufile']['name'][3];
$path5= "../uploads/".$_FILES['ufile']['name'][4];
$path6= "../uploads/".$_FILES['ufile']['name'][5];
$path7= "../uploads/".$_FILES['ufile']['name'][6];
$path8= "../uploads/".$_FILES['ufile']['name'][7];
}
$path = array();
for($i=0;$i<=7;++$i)
$path[$i]="../uploads/".$_FILES['ufile']['name'][$i];
I would advise against your current coding style. Life would be simpler if you just stored the paths in an array, e.g.
$paths[1] = "../uploads/" . $_FILES['ufile']['name'][0];
$paths[2] = "../uploads/" . $_FILES['ufile']['name'][1];
Then you could do something like this:
$paths = array();
for ($i = 0; $i <= 7; $i++) {
$paths[$i + 1] = $_FILES['ufile']['name'][$i];
}
But to answer your question, you can do something like this instead, which is very similar:
$paths = array();
for ($i = 0; $i <= 7; $i++) {
$paths['path' . ($i + 1)] = $_FILES['ufile']['name'][$i];
}
extract($paths);
See the extract doc page for more info about what's going on here
You can use variable variables as well :
foreach(range(0,7) as $index){
$varname = "path".$index;
$$varname = "../uploads/".$_FILES['ufile']['name'][$index];
}
Not sure what you want to do with those paths afterwards but here is my go at it. I would use the length of the array, assuming it doesn't always contain the same amount of file names.
$paths = array();
for($i = 0; $i < count($_FILES['ufile']['name']); $i++)
{
$paths[] = "../uploads/".$_FILES['ufile']['name'][$i];
}
I was wondering if it is possible to create unique variables in a for loop using PHP. I tried the following code but it didn't work:
$level_count = 6
for ($i=1; $i<=$level_count; $i++) {
$level_ + $i = array();
}
I would like to end up with the variables $level_1, $level_2, $level_3, $level_4, $level_5 and $level_6. How would I achieve this?
$level_count = 6
for ($i=1; $i<=$level_count; $i++) {
$l = "level" . $i;
$$l = array();
}
But Zerkms is right...
$arr = array(array(),array(),array(),array(),array(),array());
It's much easier if you use arrays for this. Try this one-liner:
$level_count = 6;
$array = array_fill_keys(array_map(function($index) {
return 'level_' . $index;
}, range(1, $level_count)), array());
var_dump($array);
Weird thing (I have no idea why you want to use it), but, just for educational purposes...
$level_count = 6;
for ($i = 1; $i <= $level_count; $i++) {
$name = 'level_' . $i;
$$name = array();
}
I have the following code:
for ($i = 1; $i <= $j; $i++)
{
$goods_{$i} = array(
$_POST["'goods'.$i'_title'"],
$_POST["'goods'.$i.'_package'"],
$_POST["'goods'.$i.'_nmr'"]
);
}
I hoped that it could make this in first step of the cycle:
$i =1;
$goods_1 = array(
$_POST['goods1_title'],
$_POST['goods1_package'],
$_POST['goods1_nmr']
);
and so on in other steps.
I follow AbraCadaver's sentiments:
Why in the world are you doing this? You are using arrays, keep using them.
As such, I would write the code simply using an Array:
$goods = array();
for ($i = 1; $i <= $j; $i++)
{
// Assign to an index in the already created array,
// but DO NOT create a new variable.
$goods[$i] = array(
// Also make sure these are correct ..
$_POST["goods{$i}_title"],
);
}
If you really want to create dynamic variables - ick! - see variable variables.
Should be
$_POST["goods{$i}_title"],
$_POST["goods{$i}_package"],
$_POST["goods{$i}_nmr"]
It can be done like this:
for ($i = 1; $i <= $j; $i++)
{
${"goods_$i"} = array(
$_POST["'goods'.$i'_title'"],
$_POST["'goods'.$i.'_package'"],
$_POST["'goods'.$i.'_nmr'"]
);
}
You can read more about this topic in related PHP documentation.
The result of "'goods'.$i'_title'" will be 'goods'.1'_title', in case that you want it to be goods1_title then use following code instead:
for ($i = 1; $i <= $j; $i++)
{
${"goods_$i"} = array(
$_POST["goods{$i}_title"],
$_POST["goods{$i}_package"],
$_POST["goods{$i}_nmr"]
);
}
Another bug might be that in 1 case you use .$i. and in other 2 cases you use .$i without the last ..
I need a way to do this
for($i=1;$i<=10;$i++){
$id$i = "example" . $i;
}
notice the second line has $id$i
so for the first loop $id1 will equal example1
in the second loop $id2 will equal example2
and so on...
Thank you very much!
You can use variable variable names to do this; however, it's probably much more convenient if you just used an array:
for($i = 1, $i <= 10, $i++) {
$id[] = "example" . $i;
}
You can convert a string into a variable (the name of the variable), if you put another $ in front of it:
$str = "number";
$number = 5;
$$str = 8;
echo $number; // will output 8
So in your example, you could do it like that:
for($i = 1; $i <= 10; $i++) {
$str_var = "id".$i;
$$str_var = "example".$i;
}
It would be much better to use an array, but you could do this:
for($i=1; $i<=10; $i++){
$var ="id$i";
$$var = "example" . $i;
}
Here's what I would recommend doing instead:
$ids = array;
for($i = 1; $i <= 10; $i++) {
$ids[$i] = "example" . $i;
}
You could create an array of size $i with a name of $id, and insert each element into a different index.
for($i=1;$i<=10;$i++){
$id[$i] = "example" . $i;
}
$var = array();
for($i=1; $i<=10; $i++) {
$var['id' . $i] = 'example' . $i;
}
extract($var, EXTR_SKIP);
unset($var);
but why not use a simple array?
i have a variables like $srange0 , $srange1, $srange2 $srange3.
i am using to declare some value to each value using for loop.
for($i=0;$i<=3;$i++){
$srange.$i = $i;
}
but its not working ?
is there any alternative solution for this
for($i=0;$i<=3;$i++){
$var = 'srange'.$i;
$$var = $i;
}
But, whenever I see variables like that, I'd use an array instead.
Use an array:
$srange = array();
for ($i = 0; $i <= 3; ++$i)
$srange[$i] = $i;
For the purpose of this particular task, you can also do this:
$srange = range(0, 3);
That also builds the same array as my first code snippet.
The properway to add these dynamic variables will be like this
for($i=0;$i<=3;$i++){
$name = 'srange'.$i;
$$name = $i;
}
This may be helpful to you:
$srange0;
$srange1;
$srange2;
for($i=0;$i<=3;$i++) {
$range = "srange".$i;
$$range = $i;
}
echo $srange2."<br />";
exit;