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


Trying to use echo’s commas instead of string concatenation

我不知道我做错了什么,但我得到了解析错误:">分析错误:语法错误,意外的','在…">

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

你能帮我吗?我不想使用串联,因为速度较慢。

Basicaly逗号分隔的值是参数。您正试图将参数传递给变量,但没有传递回显!

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

用$msg:中的.替换字符串之间的,

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

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;

不能在字符串赋值中使用逗号,逗号仅适用于echo命令本身。因此,如果你想避免上面提到的串联,你需要这样做:

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

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