Which method is used to spell strings? $ a & nbsp; aa; $ B & nbsp; bb; $ str & nbsp; {:. $ ., B :. $ B .}; & nbsp; $ str1 & nbsp; {a: $ a, B: $ B}; $ str2 & nbsp; sprintf ({a: % s, b: % s}, $ a, $ B); which method does ec use to spell strings?
$ 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 In progress
Which is better, or there are different scenarios... each has its own number?
Is there any other way to spell strings ???
------ Solution --------------------
$a = "aa";
$b = "bb";
$array = array();
$array['a'] = $a;
$array['b'] = $b;
echo json_encode($array);
Demo:
Http://sandbox.onlinephpfunctions.com/code/d3f616489979148ab4896c38b8a17b034259daab
------ Solution --------------------
I generally use sprintf when I have precision requirements.
------ Solution --------------------
$a = 'aa';
$b = 'bb';
$array = array (
'a' => $a,
'b' => $b
);
$str2 = json_encode ( $array );
echo $str2;
Suggestion: to assign a value to a string, use as few double quotation marks as possible, and use single quotation marks.