PHP string concatenation

Source: Internet
Author: User
Tags echo command

There are 2 ways to declare a string in PHP, one in single quotes and the other in double quotes. 2 The biggest difference is that when a string contains a variable, the single quotation mark cannot output the value of the variable, but instead directly output the variable name, double quotation marks can output the value of the variable.

1 <? PHP 2 $a = ' Hello '; 3 $b = ' world '; 4 Echo ' $a $b ';  // output $ A $b 5 Echo "$a$b// output Helloworld6 ?>

There are only 2 escape characters in the single quotation mark, \ ' (for single quotes) and \ \ (for backslashes), and the double quotes do not have this limit.

1 <? PHP 2 Echo // output ' Hello\world ' 3 Echo "\thello\r\n";  // \ t = tab, \ r \ n for line break 4 ?>

The concatenation of strings in PHP is used '. ', the output string is usually used with echo.

1 <? PHP 2 $a = ' Hello '; 3 $b = ' world '; 4 Echo $a $b; // Output Hello    world (&nbsp;) indicates a space 5 ?>

You can also use curly braces {$str} in the string inline variables, such as "text {$str} text", if you do not use curly braces {} to pay attention to, such as "text $str text", this is the output text "Word + $str text", rather than "text + $str + text", of course, do not have {} You can also use Word segmentation characters to separate variables and text, word breakers are generally spaces, punctuation, and so on.

1 <? PHP 2 $a = ' Hello '; 3 Echo "qqq{$a}qq"; // Output QQQHELLOQQQ 4 Echo "QQQ$aqqq"; // the output is QQQ$AQQQ, not QQQHELLOQQQ . 5 Echo "QQQ$a qqq"; // It is also possible to add a space after $ A, output qqq$a QQQ 6 ?>

In PHP It is also possible to write multiple lines of string directly

1<?PHP2 $str=<<<STR3' OK ', ' hello '4 I 'll5{$str 1}6 STR;7 8 /****9 * Three of the less than number represents the input of a multiline string, str is a string qualifier, the name of the delimiter can be defined by itself, and the content between the two qualifiers is a multiline string. The single quotation marks can be directly output without escaping, of course, can also be inserted directly inside the variable, the output will automatically replace the value of the variableTen ****/ One?>

A little trick, when using echo for string output, if the string is composed using concatenation, you can use commas to concatenate the parts of the string and variables that need to be spliced, such as

Echo ' first ', "second", $str, "end";

It is said that the output speed is faster because Echo can accept multiple parameters and output directly in order, in fact, the comma is not a concatenation of strings, but the different strings and variables as parameters to the echo command

PHP string concatenation

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.