Ok, I'm sick of encapsed string error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Ok, I'm sick of encapsed string error

Post by pilau »

Take a look:

Code: Select all

$JSarray .= "events[". $j + 1 ."]={\n";
Now what's wrong with that?
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

Try:

Code: Select all

$JSarray .= 'events['. ++$j .']={' . "\n";
I think it may not like the the "{\n"...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It has to do with the +. Operator precedence dictates that plus, minus and dot operators are processed from left to right within a given statement. Here's an illustration of what's happening

Code: Select all

[feyd@home]>php -r "$j = 2; echo 'foo'.$j+1.'bar';"
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in Command line code on line 1

[feyd@home]>php -r "$j = 2; echo 'foo'.$j;//+1.'bar';"
foo2
[feyd@home]>php -r "$j = 2; echo 'foo'.$j+1;//.'bar';"
1
[feyd@home]>php -r "$j = 2; echo 'foo'.($j+1).'bar';"
foo3bar
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Ok, so it's like math, in general. "Don't forget that braces, are like a concrete wall!" - my math teacher always said.
Post Reply