PHP将多个字符串分配给单个变量
问题描述:
是否有更干净的方法将多个字符串分配给php中的单个变量?
Is there a cleaner way to assign multiple strings to a single variable in php?
我目前确实喜欢此
<?php
$myStr = '';
$myStr .= 'John';
$myStr .= '<br>';
$myStr .= 'Paul';
$myStr .= '<br>';
$myStr .= 'Ringo';
echo $myStr;
?>
我还使用 HEREDOC
.但是还有其他方法吗?
I also use HEREDOC
. But are there other ways?
答
如果必须连接大量数据,最好使用arrays
. 更干净(不一定会提高内存效率).
If you have to concatenate lot of data, it may be a good idea to use arrays
. It's cleaner (not necessarily more memory efficient).
$items = array('Hello', 'How', 'Are', 'You?');
echo implode(' ', $items);