尝试使用echo的逗号而不是字符串连接

时间:2011-08-29 13:33:10

标签: php concatenation

我不知道我在这里做错了什么,但我得到解析错误:  “解析错误:语法错误,意外','在......

$msg= 'This is ', htmlentities($from, ENT_QUOTES, 'UTF-8'),' and ', htmlentities($to, ENT_QUOTES, 'UTF-8'),' dates statistic ';
echo $msg;
你能帮帮我吗?我不想使用连接,因为速度较慢。

5 个答案:

答案 0 :(得分:3)

基本上逗号分隔的值是参数。您正在尝试将参数传递给变量但不传递回应!

echo 'This is ',
  htmlentities($from, ENT_QUOTES, 'UTF-8'),
  ' and ',
  htmlentities($to, ENT_QUOTES, 'UTF-8'),
  ' dates statistic ';

答案 1 :(得分:2)

在$ msg:{/ p>中用,替换字符串之间的.

$msg= 'This is ' . htmlentities($from, ENT_QUOTES, 'UTF-8') . ' and ' . 
      htmlentities($to, ENT_QUOTES, 'UTF-8') . ' dates statistic ';

或直接回声:

echo 'This is ', htmlentities($from, ENT_QUOTES, 'UTF-8'),' and ', htmlentities($to, ENT_QUOTES, 'UTF-8'),' dates statistic ';

答案 2 :(得分:2)

echo接受用逗号分隔的多个值,而变量赋值则不接受。

将起作用

echo 'This is ', htmlentities($from, ENT_QUOTES, 'UTF-8'),' and ', htmlentities($to, ENT_QUOTES, 'UTF-8'),' dates statistic ';

$msg= 'This is '. htmlentities($from, ENT_QUOTES, 'UTF-8') . ' and ' . htmlentities($to, ENT_QUOTES, 'UTF-8') . ' dates statistic ';
echo $msg;

答案 3 :(得分:2)

您不能在字符串赋值中使用逗号,逗号仅适用于echo命令本身。因此,如果您想避免连接,如上所述,您需要这样做:

echo  'This is ', htmlentities($from, ENT_QUOTES, 'UTF-8'),
      ' and ', htmlentities($to, ENT_QUOTES, 'UTF-8'),' dates statistic ';

答案 4 :(得分:1)

echo 'This is ', htmlentities($from, ENT_QUOTES, 'UTF-8'),' and ', htmlentities($to, ENT_QUOTES, 'UTF-8'),' dates statistic ';

逗号仅适用于echo,而不适用于变量赋值。