Which method is used to spell strings?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.