FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Why Can't I "define" a Value for a Subscript?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Why Can't I "define" a Value for a Subscript? [message #175235] Sun, 28 August 2011 19:18 Go to next message
eBob.com is currently offline  eBob.com
Messages: 11
Registered: August 2011
Karma: 0
Junior Member
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
Re: Why Can't I "define" a Value for a Subscript? PS [message #175236 is a reply to message #175235] Sun, 28 August 2011 19:36 Go to previous messageGo to next message
eBob.com is currently offline  eBob.com
Messages: 11
Registered: August 2011
Karma: 0
Junior Member
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(dot)com(at)totallybogus(dot)com> wrote in message
news:Cvw6q(dot)176718$k33(dot)110045(at)en-nntp-13(dot)dc1(dot)easynews(dot)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
Re: Why Can't I "define" a Value for a Subscript? [message #175237 is a reply to message #175235] Sun, 28 August 2011 19:43 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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
Re: Why Can't I "define" a Value for a Subscript? [message #175238 is a reply to message #175235] Sun, 28 August 2011 19:48 Go to previous messageGo to next message
Chuck Anderson is currently offline  Chuck Anderson
Messages: 63
Registered: September 2010
Karma: 0
Member
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
*****************************
Re: Why Can't I "define" a Value for a Subscript? PS [message #175239 is a reply to message #175236] Sun, 28 August 2011 19:53 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
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(dot)com(at)totallybogus(dot)com> wrote in message
> news:Cvw6q(dot)176718$k33(dot)110045(at)en-nntp-13(dot)dc1(dot)easynews(dot)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?-
Re: Why Can't I "define" a Value for a Subscript? [message #175240 is a reply to message #175235] Sun, 28 August 2011 20:02 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
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
Re: Why Can't I "define" a Value for a Subscript? [message #175241 is a reply to message #175238] Sun, 28 August 2011 20:57 Go to previous messageGo to next message
eBob.com is currently offline  eBob.com
Messages: 11
Registered: August 2011
Karma: 0
Junior Member
"Chuck Anderson" <cycletourist(at)invalid(dot)invalid> wrote in message
news:j3e62a$p8k$1(at)dont-email(dot)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
Re: Why Can't I "define" a Value for a Subscript? [message #175243 is a reply to message #175241] Mon, 29 August 2011 00:26 Go to previous messageGo to next message
Richard Damon is currently offline  Richard Damon
Messages: 58
Registered: August 2011
Karma: 0
Member
On 8/28/11 4:57 PM, eBob.com wrote:
>
> "Chuck Anderson" <cycletourist(at)invalid(dot)invalid> wrote in message
> news:j3e62a$p8k$1(at)dont-email(dot)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).
Re: Why Can't I "define" a Value for a Subscript? PS [message #175253 is a reply to message #175236] Mon, 29 August 2011 11:43 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
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
--
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 Go to previous messageGo to next message
A.Reader is currently offline  A.Reader
Messages: 15
Registered: December 2010
Karma: 0
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.
Re: Why Can't I "define" a Value for a Subscript? [message #175262 is a reply to message #175260] Tue, 30 August 2011 15:09 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/30/2011 9:55 AM, A.Reader wrote:
> 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.

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(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Deploying PHP 5.3 on CentOS 5.6 with Puppet
Next Topic: Strategic Marketing Summit 2011 (September 24th,Chennai)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 25 22:17:45 GMT 2024

Total time taken to generate the page: 2.05780 seconds