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


Groups > comp.lang.php > #2982 > unrolled thread

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

Started by"eBob.com" <eBob.com@totallybogus.com>
First post2011-08-28 15:18 -0400
Last post2011-08-30 11:09 -0400
Articles 11 — 9 participants

Back to article view | Back to comp.lang.php


Contents

  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

#2982 — Why Can't I "define" a Value for a Subscript?

From"eBob.com" <eBob.com@totallybogus.com>
Date2011-08-28 15:18 -0400
SubjectWhy Can't I "define" a Value for a Subscript?
Message-ID<Cvw6q.176718$k33.110045@en-nntp-13.dc1.easynews.com>
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?

Thanks,  Bob 

[toc] | [next] | [standalone]


#2983 — Re: Why Can't I "define" a Value for a Subscript? PS

From"eBob.com" <eBob.com@totallybogus.com>
Date2011-08-28 15:36 -0400
SubjectRe: Why Can't I "define" a Value for a Subscript? PS
Message-ID<5Mw6q.146894$fD6.34307@en-nntp-02.dc1.easynews.com>
In reply to#2982
I did a simple experiment that I thought might shed some light on this.  But 
instead I just increased my confusion.

I tried the following ...

define('SS1SITE',0);
echo 'ss1site is: '. "SS1SITE\n";

and the output is ...

ss1site is: SS1SITE

My book says that variable substitution happens within double quotes and in 
my limited experience that is true.

Why isn't the variable substitution happening in this case?

Thanks,  Bob

"eBob.com" <eBob.com@totallybogus.com> wrote in message 
news:Cvw6q.176718$k33.110045@en-nntp-13.dc1.easynews.com...
>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?
>
> Thanks,  Bob 

[toc] | [prev] | [next] | [standalone]


#2986 — Re: Why Can't I "define" a Value for a Subscript? PS

FromNorman Peelman <npeelman@cfl.rr.com>
Date2011-08-28 15:53 -0400
SubjectRe: Why Can't I "define" a Value for a Subscript? PS
Message-ID<j3e6b2$qsm$1@dont-email.me>
In reply to#2983
On 08/28/2011 03:36 PM, eBob.com wrote:
> I did a simple experiment that I thought might shed some light on this.
> But instead I just increased my confusion.
>
> I tried the following ...
>
> define('SS1SITE',0);
> echo 'ss1site is: '. "SS1SITE\n";
>
> and the output is ...
>
> ss1site is: SS1SITE
>
> My book says that variable substitution happens within double quotes and
> in my limited experience that is true.
>
> Why isn't the variable substitution happening in this case?
>
> Thanks, Bob
>
> "eBob.com" <eBob.com@totallybogus.com> wrote in message
> news:Cvw6q.176718$k33.110045@en-nntp-13.dc1.easynews.com...
>> 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?
>>
>> Thanks, Bob
>

   You can. Try:

echo 'we have a match - key: ',$file2cols[SS2KEY],"\n";

or

echo "we have a match - key: {$file2cols[SS2KEY]}\n";

   You must use curly braces around (multi-dimensional) array variables 
and constant keys in strings.


-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

[toc] | [prev] | [next] | [standalone]


#3000 — Re: Why Can't I "define" a Value for a Subscript? PS

From"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid>
Date2011-08-29 13:43 +0200
SubjectRe: Why Can't I "define" a Value for a Subscript? PS
Message-ID<j3fu18$tq9$1@dont-email.me>
In reply to#2983
El 28/08/2011 21:36, eBob.com escribió/wrote:
> I did a simple experiment that I thought might shed some light on this.
> But instead I just increased my confusion.
>
> I tried the following ...
>
> define('SS1SITE',0);
> echo 'ss1site is: '. "SS1SITE\n";
>
> and the output is ...
>
> ss1site is: SS1SITE
>
> My book says that variable substitution happens within double quotes and
> in my limited experience that is true.
>
> Why isn't the variable substitution happening in this case?

Because *constants* are not *variables*.

http://www.php.net/manual/en/language.variables.php
http://www.php.net/manual/en/language.constants.php


 >>> Thank you!
 >> in inverse order.
 > please do not quote
By the way,


-- 
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

[toc] | [prev] | [next] | [standalone]


#2984

FromDenis McMahon <denis.m.f.mcmahon@gmail.com>
Date2011-08-28 19:43 +0000
Message-ID<4e5a9a7e$0$2898$a8266bb1@newsreader.readnews.com>
In reply to#2982
On Sun, 28 Aug 2011 15:18:32 -0400, eBob.com wrote:

> 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";

Try:

echo "we have a match - key: {$file2cols[SS2KEY]}\n";

This php:

<?php
define('ZERO',0);
define('SZERO','zero');
$bloop = array();
$bloop[ZERO] = 0;
$bloop[SZERO] = 'nought';
echo "{$bloop[ZERO]},{$bloop[SZERO]}\n";
?>

Gives this output:

$ php constants.php
0,nought
$

Rgds

Denis McMahon

[toc] | [prev] | [next] | [standalone]


#2985

FromChuck Anderson <cycletourist@invalid.invalid>
Date2011-08-28 13:48 -0600
Message-ID<j3e62a$p8k$1@dont-email.me>
In reply to#2982
eBob.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?
>
> Thanks, Bob

Php does not look for constants within double quotes.

http://www.php.net/manual/en/language.types.array.php
(Look for "Array do's and don'ts" - scroll down a wee bit for examples)

-- 
*****************************
 Chuck Anderson • Boulder, CO
  http://cycletourist.com
 Turn Off, Tune Out, Drop In
*****************************

[toc] | [prev] | [next] | [standalone]


#2988

From"eBob.com" <eBob.com@totallybogus.com>
Date2011-08-28 16:57 -0400
Message-ID<uYx6q.176728$k33.132762@en-nntp-13.dc1.easynews.com>
In reply to#2985
"Chuck Anderson" <cycletourist@invalid.invalid> wrote in message 
news:j3e62a$p8k$1@dont-email.me...
> eBob.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?
>>
>> Thanks, Bob
>
> Php does not look for constants within double quotes.
>
> http://www.php.net/manual/en/language.types.array.php
> (Look for "Array do's and don'ts" - scroll down a wee bit for examples)
>
> -- 
> *****************************
> Chuck Anderson • Boulder, CO
>  http://cycletourist.com
> Turn Off, Tune Out, Drop In
> *****************************

OUCH!!!!!!!   But thanks!  Now if I can only remember this "feature".

Thanks,  Bob 

[toc] | [prev] | [next] | [standalone]


#2990

FromRichard Damon <news.x.richarddamon@xoxy.net>
Date2011-08-28 20:26 -0400
Message-ID<j3emb2$sql$1@dont-email.me>
In reply to#2988
On 8/28/11 4:57 PM, eBob.com wrote:
>
> "Chuck Anderson" <cycletourist@invalid.invalid> wrote in message
> news:j3e62a$p8k$1@dont-email.me...
>> eBob.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?
>>>
>>> Thanks, Bob
>>
>> Php does not look for constants within double quotes.
>>
>> http://www.php.net/manual/en/language.types.array.php
>> (Look for "Array do's and don'ts" - scroll down a wee bit for examples)
>>
>> --
>> *****************************
>> Chuck Anderson • Boulder, CO
>> http://cycletourist.com
>> Turn Off, Tune Out, Drop In
>> *****************************
>
> OUCH!!!!!!! But thanks! Now if I can only remember this "feature".
>
> Thanks, Bob

To understand why it is this way, just think about this program:

define('Hello', 'Goodbye cruel');
echo "Hello World";

Just looking at the echo statement, you should know what is printed, and 
not need to scan the whole program to make sure nothing is going to 
change your string.

if you see

echo "Hello $name";

the $ make it clear that the following will be substituted so there is 
no surprise (if you know the language rules).

[toc] | [prev] | [next] | [standalone]


#2987

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2011-08-28 15:02 -0500
Message-ID<slrnj5l7ms.icc.hellsop@nibelheim.ninehells.com>
In reply to#2982
On Sun, 28 Aug 2011 15:18:32 -0400, eBob.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?

Try the following right after the failing example:

print "This is a quoted constant: SS2KEY\n";
$foo=$file2cols[SS2KEY];
print "this is a variable assigned: $foo\n";
print "this is concat test: ".$file2cols[SS2KEY]."\n";

Then come back and tell us if you want to change your question.

-- 
I still want a phone with caller-IQ.
			    -- Tanuki in the monastery

[toc] | [prev] | [next] | [standalone]


#3008

FromA.Reader <anonymously@example.com>
Date2011-08-30 09:55 -0400
Message-ID<43qp57h6jmbi8jfj5be1iomooivev1654j@4ax.com>
In reply to#2982
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.

[toc] | [prev] | [next] | [standalone]


#3010

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-08-30 11:09 -0400
Message-ID<j3iufr$e2a$1@dont-email.me>
In reply to#3008
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
==================

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web