PHP string connector instance

Source: Internet
Author: User
In most cases, we need to connect several strings and display them. in PHP, strings are connected by "points", that is, periods in English & quot;. & quot ;. & Quot;. & quot; is a string connector. you can link two or more strings into one string. In most cases, we need to connect several strings and display them. in PHP, strings are connected by "points", that is, periods in English. "." . "." Is a string connector. you can link two or more strings into one string. For example:

 

Output result:

PHP: www.php1.cn

The string connection symbol cannot be used to connect a large number of strings. PHP allows programmers to directly include string variables in double quotes. when the echo statement is followed by double quotes, you can use the following format to achieve the same effect. For example:

 

Output result:

PHP: www.php1.cn

String echo output comma can also be used as a php connector

When multiple strings are output, use commas instead of periods to separate strings, which is faster. The same results can be obtained in the following four methods, but only 4th are the best.

 ";echo "Hello " . $foo . " welcome to phpcn.";echo "
";echo 'Hello ' . $foo . ' welcome to phpcn.';echo "
";echo 'Hello ', $foo , ' welcome to phpcn.';?>

Note:

The first comma "," cannot run, because using double quotation marks will force php to search for replacement values for this string.

The second is better, because php does not need to be replaced.

Third, it is better because single quotes are used, so the language knows that you can transfer text out of the process, but the "bad" thing is that you use a connector (no operation, just like the second example ).

The last one uses single quotes and adds a connector. Why is this best?

Let's first look at the third case. php creates a string containing "Hello", expands it, adds the foo variable ("Tom"), and expands it again, add "welcome to phpcn. then, echo can use it.

However, the fourth case is that the only thing echo does is to send "Hello", then output the content of $ foo, and then output "welcome to phpcn .". Because echo only sends text without creating a string that needs to be expanded.

Here, we should understand that optimizing the php string connector can speed up the program running. It is also found that the comma (,) can also be used as a php connector. generally, a comma (.) is used as a connector. Is it amazing?

It is said that this may be the habit of starting from C. the output of printf is variable parameter, and echo inherits this excellent tradition. However, you need to understand that the comma here is the parameter delimiter, not the string connector.

Let's talk about the difference between commas and periods.

echo $str,$str2,$str3;

The results are output one by one using commas.

While

echo $str.$str2.$str3;

The vertex number is used to calculate all strings and then output the result.

In this case, the efficiency of the comma is higher than that of the dot. Because comma-separated connections do not require computation.

The above is the details about the use of PHP string connectors. For more information, see other related articles in the first PHP community!

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.