Page 1 of 1

Writing a simple statement with an index

Posted: Fri May 21, 2010 7:30 pm
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?

Re: Writing a simple statement with an index

Posted: Sat May 22, 2010 12:06 am
by requinix
Because variables don't work in single-quoted strings.

Code: Select all

"add[$i]"