Writing a simple statement with an index

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
makamo66
Forum Newbie
Posts: 24
Joined: Wed May 12, 2010 6:13 pm

Writing a simple statement with an index

Post by makamo66 »

I can write this and get the right results:

if ( $_SESSION['addId'] == 'add[4]' )
$productId = 1;
if ( $_SESSION['addId'] == 'add[5]' )
$productId = 2;
if ( $_SESSION['addId'] == 'add[6]' )
$productId = 3;

But if I try to streamline it by writing it this way, it doesn't work:

if ( $_SESSION['addId'] == 'add[$i]' )
$productId = $i;

I think the two methods should be equivalent so why doesn't the second way work?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Writing a simple statement with an index

Post by requinix »

Because variables don't work in single-quoted strings.

Code: Select all

"add[$i]"
Post Reply