Home >Backend Development >PHP Tutorial >PHP string concatenation_PHP tutorial

PHP string concatenation_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:45:501047browse

Many times we need to connect several strings for display. In PHP, "dots" are used to connect strings, which is the period "." in English. The specific usage is as follows:

//Define string

$str1 = "Hello World!";

$str2 = "Welcome to HutaoW's BLOG!";

//Concatenate the two strings above and separate them with spaces

$str3 = $str1 . " " . $str2;

//Output the connected string

echo $str3;

/*

After this code is executed, the browser page will display

"Hello World! Welcome to HutaoW's BLOG!"

*/

?>

There is another way to connect strings, which is a bit like the placeholder of "printf" in C, but PHP writes the variable name directly to the placeholder. The method of use is to add curly brackets before and after the variable. When displayed, the part of the string that is not enclosed by curly brackets will still be output directly, and the enclosed part will be replaced and output with the corresponding string according to the variable name. You can look at the following example to make it clearer, pay attention to the underlined part:

//Define the string to be inserted

$author = "HutaoW";

//Generate new string

$str = "Welcome to {$author}'s BLOG!";

//Output $str string

echo $str; /*

After this code is executed, the browser page will display

"Welcome to HutaoW's BLOG!"

*/

?>

Excerpted from: Shine’s Holy Paradise

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478624.htmlTechArticleMany times we need to connect several strings for display. In PHP, dots are used between strings. Connection, which is the period in English. The specific usage is as follows: ?php //Definition...
Statement:
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