Php assigning multiple strings to a single variabl

2019-07-04 06:25发布

Is there a cleaner way to assign multiple strings to a single variable in php?

I current do like this

<?php
$myStr = '';

$myStr .= 'John';
$myStr .= '<br>';
$myStr .= 'Paul';
$myStr .= '<br>';
$myStr .= 'Ringo';

echo $myStr;
?>

I also use HEREDOC. But are there other ways?

2条回答
戒情不戒烟
2楼-- · 2019-07-04 07:03

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);
查看更多
等我变得足够好
3楼-- · 2019-07-04 07:06

It can be done by array and implode() like below

$names = array('John', 'Paul', 'Ringo');
$myStr = implode("<br>", $array);
echo $myStr;
查看更多
登录 后发表回答