Re: Why Can't I "define" a Value for a Subscript? [message #175260 is a reply to message #175235] |
Tue, 30 August 2011 13:55 |
A.Reader
Messages: 15 Registered: December 2010
Karma:
|
Junior Member |
|
|
On Sun, 28 Aug 2011 15:18:32 -0400,
"eBob.com" <eBob(dot)com(at)totallybogus(dot)com> wrote:
> I am working with some csv files. To make the program more maintainable and
> more self-documenting I "define" values to use as subscripts; e.g. ...
>
> define('SS2KEY',0);
>
> BUT, whereas this produces what I expected ...
>
> echo "we have a match - key: $file2cols[0]\n";
>
> ... this does not ...
>
> echo "we have a match - key: $file2cols[SS2KEY]\n";
>
> I see the other text but no value is substitited for $file2cols[SS2KEY]
>
> I've wasted so much time on this. Why can't I define constants to use as
> subscripts?
Suggestion: don't use double quotes except when you want some
control char such as "\n" interpreted. You get some other
interpretation 'for free', but it's almost never really for free,
as others have indicated.
A better way is to use single quotes around the string bits and
leave non-string bits 'raw', gluing everything together with the
string-concat operator (.). So your example would work fine if
you did it this way:
echo 'we have a match - key: '.$file2cols[SS2KEY].'<br>' ;
echo will evaluate vars and consts correctly without needing any
garnish as long as they're not inside a string.
Hope that helps.
|
|
|