Forum Moderators: coopster

Message Too Old, No Replies

Array syntax question:

What's with $the['dot.']?

         

bedlam

6:00 am on Jun 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hiya folks,

As a self-taught err... 'programmer,'* I often find holes in my knowledge...I'm going through a piece of code in an effort to patch some of the holes, and I keep seeing essentially this construct:

$someVariable = $someOther['variable[b][4].[/4][/b]'];

I can see it's an array, but what's with the dot inside the quotes after the word "variable"?

-B

*This word meant to be understood in a way that carries no sense of 'expertise'... ;-)

[edit]I don't know what 'synatx' is either...[/edit]

HelenDev

3:27 pm on Jun 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the dot means it's appending the variable 'variable'

e.g
$myvar = "the cat";
$myvar.= " sat on the mat";

// $myvar = "the cat sat on the mat"

bedlam

4:13 pm on Jun 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Naw, I know about using the dot for concatenation in these kinds of syntax:

$variable = 'this' . ' and' . ' that';

$variable = 'this';
$variable .= ' and that';

I'm wondering about the syntax in the first post.

-B

HelenDev

4:16 pm on Jun 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just figured it was the same thing.

I'm a self-taught err... 'programmer,' too ;)

bedlam

4:30 pm on Jun 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hm. Well in fact, I have no good reason at all to think that you're wrong... It's just that in the code I'm looking at I can't tell if that's what it's doing or not...

-B

jatar_k

7:39 pm on Jun 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it doesn't really make much sense to me either ;)

I am not sure what they are trying to do, if you dump the array is that just an element/var name?

ergophobe

4:15 pm on Jun 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Based on the manual, an array index can be a string. That is, unlike a variable name, it seems that any string can be used as an array index. Without having tried it, it appears then that

$array['@48.']

is perfectly valid and doesn't mean anything special except that it is a different element than

$array['%[][][][.']

Obviously, if you take away the single quotes, you will get a fatal error.

Tom

coopster

3:22 pm on Jun 21, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Exactly. A key may be either an integer or a string [php.net], so the dot/period (.) is valid syntax. It can get pretty ugly sometimes, consider this...
mysql_query('SET @id =0;');  
$sql = 'SELECT @id := @id +1';
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
print_r($row);
// returns:
Array
(
[@id := @id +1] => 1
)

Yuck. I like to keep my array names a bit more tidy and sensible, usually without any punctuation. But, to each their own...