I am looking to do something like the following:
for ($i=0; $i<=$number_of_bundles; $i++) {
$rug_size . $i = $_POST['Size' . $i];
}
Unfortunately this does not work since I get "undefined index" and when I try echoeing, for example, $rug_size1 is undefined.
How can something like this be done?
Assuming you initialize $rug_size
as an array()
:
$rug_size[$i] = $_POST['Size' . $i];
If you really want to use different variable names (hopefully not):
$vn = 'rug_size' . $i;
$$vn = $_POST['Size' . $i];
if i am not wrong than
$_POST['Size' , $i]
will be(i.e. dot instead of comma)
$_POST['Size'. $i]
another way to avoid error:use isset()
for ($i=0; $i<=$number_of_bundles; $i++) {
if(isset($_POST['Size' . $i]))
{
$rug_size . $i = $_POST['Size' . $i];
}
}
You need wrap you dynamic variable within {} (and concat string with Dot), like:
<?php
$i = 1;
${"rug_size".$i} = "Hello world!";
echo $rug_size1;
?>
Output:
Hello world!
schould work like this:
for ($i=0; $i<=$number_of_bundles; $i++) {
$varname = 'rug_size'.$i;
$$varname = $_POST['Size'.$i];
}
do like this .
for ($i=0; $i<=$number_of_bundles; $i++) {
${'rug_size' . $i} = $_POST['Size' . $i];
}