通常拼字符串用哪种方式呀

WBOY
Release: 2016-06-13 12:55:08
Original
819 people have browsed it

一般拼字符串用哪种方式呀
$a = "aa";
$b = "bb";
$str = "{a:".$a.",b:".$b."}"; 

$str1 = "{a:$a,b:$b}";

$str2 = sprintf("{a:%s,b:%s}",$a,$b);
echo $str;
echo $str1;
echo $str2;

3中了  
一般哪种好些,或者说是有不同的场景.... 各有各的号

还有其他的拼字符串的方式吗???


------解决方案--------------------
$a = "aa";<br />
$b = "bb";<br />
$array = array();<br />
$array['a'] = $a;<br />
$array['b'] = $b;<br />
echo json_encode($array);
Copy after login


demo:
http://sandbox.onlinephpfunctions.com/code/d3f616489979148ab4896c38b8a17b034259daab
------解决方案--------------------
有精度需求时,我一般用sprintf。
------解决方案--------------------
<br />
$a = 'aa';<br />
$b = 'bb';<br />
$array = array (<br />
		'a' => $a,<br />
		'b' => $b <br />
);<br />
$str2 = json_encode ( $array );<br />
echo $str2;<br />
Copy after login


建议:给字符串赋值尽量少用双引号,用单引号。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template