开发者

Why does this code produce "syntax error, unexpected '='"?

开发者 https://www.devze.com 2023-01-16 06:07 出处:网络
$text . = \'1 paragraph\'; $text . = \'2 paragraph\'; $text . = \'3 paragraph\'; echo $text; This code gives error syn开发者_JAVA技巧tax error, unexpected \'=\'.
$text . = '1 paragraph';
$text . = '2 paragraph';
$text . = '3 paragraph';
echo $text;

This code gives error syn开发者_JAVA技巧tax error, unexpected '='.

What is the problem?


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 =


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


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


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.


Also can echo like this

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

0

精彩评论

暂无评论...
验证码 换一张
取 消