开发者

Trying to use echo’s commas instead of string concatenation

开发者 https://www.devze.com 2023-03-31 06:22 出处:网络
I don\'t know what I\'m doing wrong here, but I\'m getting pars开发者_如何学运维e error: \"Parse error: syntax error, unexpected \',\' in...\"

I don't know what I'm doing wrong here, but I'm getting pars开发者_如何学运维e error: "Parse error: syntax error, unexpected ',' in..."

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

Can you help me? I don't want to use concatenation, because of slower speed.


Basicaly commas seperated values are arguments. You are trying to pass arguments to the variable but not echo!

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


Replace , between the strings by . in the $msg:

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

or echo directly:

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


echo accepts multiple values separted with commas, variable assignation does not.

that will work

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

or

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


You can't use the commas in the string assignment, the commas only work for the echo command itself. So if you want to avoid concatenation as you mentioned above you need to do this:

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

The commas only work with echo, not with variable assignment.

0

精彩评论

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

关注公众号