Many times we need to concatenate a few strings to show that in PHP, strings are connected using "dots", which is the half-width period in English.
" . "
。 "." is a string connector that can link two or more than two strings into a single string. For example:
<?php$name = "topic.alibabacloud.com:"; $url = "www.php"; Echo $name. $url. ". CN";? >
The output is:
topic.alibabacloud.com:www.php.cn
There is no way to apply a string connection symbol to a large number of strings, PHP allows programmers to include string variables directly in double quotes, and when the Echo statement is followed by a double quotation mark (""), the following format can be used to achieve the same effect. For example:
<?php$name = "topic.alibabacloud.com:"; $url = "www.php"; echo "$name $url.cn"; The variable in double quotes is automatically distinguished from the normal string?>
The output is:
topic.alibabacloud.com:www.php.cn
string echo output comma can also be used as PHP connector
When outputting multiple strings, it is faster to separate the strings with commas instead of periods. There are 4 ways to get the same results, but only the 4th is the best.
<?php$foo = ' Tom '; echo "Hello $foo, Welcome to PHPCN."; echo "<br>"; echo "Hello". $foo. "Welcome to PHPCN."; echo "<br>"; Echo ' Hello '. $foo. ' Welcome to PHPCN. '; echo "<br>", Echo ' Hello ', $foo, ' Welcome to PHPCN ';? >
Description
The first comma "," cannot run because using double quotes forces PHP to look for a replacement value for this string.
The second is better because PHP does not have to be replaced by a need to execute.
The third is better because the single quotation marks are used, so the language knows that you can transfer text out of the process, but the "bad" thing is to use the connector (no action, just the same as the second example).
The last one uses single quotes and adds a connector. Why is this best handled?
Let's take a look at the third case, PHP creates a string, contains "Hello", then expands it, adds the Foo variable ("Tom"), and then expands it again, plus "Welcome to PHPCN." statement, and then echo can use it.
Fourth, however, the only thing Echo does is send "Hello", then output the contents of $foo, and then output "Welcome to PHPCN." Because echo simply sends text without creating a string that needs to be expanded.
Here, you should understand the optimization of the PHP string connector, you can improve the speed of the program. and found that the original "," comma can also be used as a PHP connector, usually we are generally using a little "." As a connector. Does it feel magical?
It is said that this is probably the habit from the beginning of C, the output of printf is a variable parameter, Echo also inherited this fine tradition. However, it should be understood that the comma here is a parameter spacer, not a string connector.
The difference between a comma and a period
Echo $str, $str 2, $str 3;
Operations using commas are output-by-operation results
and
Echo $str. $str 2. $str 3;
Using the dot number is the first operation of all the strings to get the result and then output.
The efficiency of the comma is, of course, higher than the dot. Because a comma connection is not required to operate.