views:

112

answers:

5
$text . = '1 paragraph';
$text . = '2 paragraph';
$text . = '3 paragraph';
echo $text;

This code gives error syntax error, unexpected '='.

What is the problem?

+4  A: 

The space between the dot and the equal? .= instead of . =

Palantir
still doesn't work, says "syntax error, unexpected '='"
Happy
@Happy this should work. `$text .= '1 paragraph'`
Pekka
Happy, Are you sure the syntax error is coming form that command and not anywhere is, concatenation is the raw basics of programming and using the line Pekka stated WILL work.
RobertPitt
+4  A: 

If you are going to output all of that anyway, then why concatenate at all? Just echo it:

echo '1 paragraph', 
     '2 paragraph',
     '3 paragraph';
Gordon
comma(,) is faster than dot(.) for achieving concatenation in echo
Web Developer
@WebDeveloper Using `,` is not concatenating at all. It's just passing multiple arguments to `echo` and sending these directly to the output. When you use `.` you concatenate the arguments *before* passing them to `echo`. Whether you use `,` or `.` here is a µ-optimization not worth mentioning.
Gordon
+7  A: 

I think you want:

$text = '1 paragraph';
$text .= '2 paragraph';
$text .= '3 paragraph';
echo $text;

Note that the first line does not use .=, and just uses =

Jonathan Fingland
true answer, thanks man.
Happy
@Happy: Happy to help
Jonathan Fingland
+1  A: 

Also can echo like this

echo '1 paragraph'.'2 paragraph'.'3 paragraph';

Wazzy
+2  A: 

Others have already pointed out the error: space between . and =.

This is a syntax/parse error. When PHP sees the . followed by space it takes . as a separate token which is used for string concatenation. Now it expects a string or a variable after it. But when it sees the = it throws the parse error as it does not match the PHP grammar.

codaddict