single quotes vs double quotes for dynamic var names

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

single quotes vs double quotes for dynamic var names

Post 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;
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post 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;
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

it's the same as echo "foo$bar" and echo 'foo$bar'.

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