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 ( ) 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