Page 1 of 1

single quotes vs double quotes for dynamic var names

Posted: Tue Feb 08, 2005 4:21 pm
by Burrito
why does this work:

Code: Select all

for($i=0;$i<4;$i++)&#123;
echo $_POST&#1111;"myvar$i"];
&#125;
but this doesn't?

Code: Select all

for($i=0;$i<4;$i++)&#123;
echo $_POST&#1111;'myvar$i'];
&#125;

Posted: Tue Feb 08, 2005 4:25 pm
by PrObLeM
because single quotes dont parse variables

$_POST['myvar'.$i] will work

may i suggest, instead of making it daynamic variables put all the variables in to an array like so

$_POST[myArrayofVariables][$i] = someValue;


then you can do a

Code: Select all

foreach($_POST&#1111;myArrayofVariables] as $foo => $bar) &#123;

echo $foo . ' = ' . $bar . "<br>";

&#125;

Posted: Tue Feb 08, 2005 4:46 pm
by timvw
it's the same as echo "foo$bar" and echo 'foo$bar'.

my favorite is echo "foo{$bar}foo";