Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3010

Re: Why Can't I "define" a Value for a Subscript?

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: Why Can't I "define" a Value for a Subscript?
Date 2011-08-30 11:09 -0400
Organization A noiseless patient Spider
Message-ID <j3iufr$e2a$1@dont-email.me> (permalink)
References <Cvw6q.176718$k33.110045@en-nntp-13.dc1.easynews.com> <43qp57h6jmbi8jfj5be1iomooivev1654j@4ax.com>

Show all headers | View raw


On 8/30/2011 9:55 AM, A.Reader wrote:
> On Sun, 28 Aug 2011 15:18:32 -0400,
> "eBob.com"<eBob.com@totallybogus.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.

That's one way to do it.  But a lot of people, myself included, detest 
the complete mishmash such coding creates.  I think the following is 
much clearer:

echo "we have a match - key: {$file2cols[SS2KEY]}<br>";

And fewer quotes means less chance of mismatched quotes, which can still 
happen even with an intelligent editor.

Another way (which I like less but has its uses) would be:

printf('we have a match - key: %s<br>', $file2cols[SS2KEY]);

This can be handy when you have a lot of values, some of them being the 
result of function calls (i.e. mysql_real_escape_string()).

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Why Can't I "define" a Value for a Subscript? "eBob.com" <eBob.com@totallybogus.com> - 2011-08-28 15:18 -0400
  Re: Why Can't I "define" a Value for a Subscript?  PS "eBob.com" <eBob.com@totallybogus.com> - 2011-08-28 15:36 -0400
    Re: Why Can't I "define" a Value for a Subscript?  PS Norman Peelman <npeelman@cfl.rr.com> - 2011-08-28 15:53 -0400
    Re: Why Can't I "define" a Value for a Subscript?  PS "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2011-08-29 13:43 +0200
  Re: Why Can't I "define" a Value for a Subscript? Denis McMahon <denis.m.f.mcmahon@gmail.com> - 2011-08-28 19:43 +0000
  Re: Why Can't I "define" a Value for a Subscript? Chuck Anderson <cycletourist@invalid.invalid> - 2011-08-28 13:48 -0600
    Re: Why Can't I "define" a Value for a Subscript? "eBob.com" <eBob.com@totallybogus.com> - 2011-08-28 16:57 -0400
      Re: Why Can't I "define" a Value for a Subscript? Richard Damon <news.x.richarddamon@xoxy.net> - 2011-08-28 20:26 -0400
  Re: Why Can't I "define" a Value for a Subscript? "Peter H. Coffin" <hellsop@ninehells.com> - 2011-08-28 15:02 -0500
  Re: Why Can't I "define" a Value for a Subscript? A.Reader <anonymously@example.com> - 2011-08-30 09:55 -0400
    Re: Why Can't I "define" a Value for a Subscript? Jerry Stuckle <jstucklex@attglobal.net> - 2011-08-30 11:09 -0400

csiph-web