Four Methods for defining strings in PHP are described in detail, and four methods are described in detail.
1. Introduction
In PHP, this language is a weak type language, so you do not need to define variables in advance.
When we use php for development, most of them use double quotation marks and single quotation marks to define strings. Since there are two ways, there must be a difference between them.
In addition to defining strings with single double quotes, php also adds the syntax sugar heredoc and nowdoc.
2. single quotation marks and double quotation marks
2.1. Single quotes
In single quotes, any special characters are output as they are [except \, \ 'will be escaped]
Eg.
<?php$var = 'this is a var!';echo '666$var666、\'、\"、\\、\n、\r、\t、\$';
From the above example, we can see that the variables are not parsed. At the same time, escape characters with backslashes only have single quotes, backslashes are escaped, and others are directly output.
2.2 double quotation marks
In double quotation marks, $ will parse the variable;
Double quotation marks will escape the escape characters of the backslash;
<?php$var = 'this is a var!'; echo "666$var 666、\'、\"、\\、\n、\r、\t、\$";
Escape Character Code |
Meaning of escape characters |
\" |
Double quotation marks |
\' |
Single quotes |
\\ |
Backslash |
\ N |
Line Break |
\ R |
Carriage Return |
\ T |
Tab |
\ $ |
Dollar sign |
As shown in the preceding example, variables in double quotation marks are parsed and replaced with strings. escape characters with backslashes are escaped accordingly.
2.3. Differences between Single and Double quotation marks
The main differences between the two are as follows:
1. Double quotation marks Replace the variable value, while single quotation marks output the variable as a string.
2. Support for escape
3. Performance speed problems. Because strings in double quotation marks need to detect variables with the $ symbol, in theory, single quotation marks are relatively fast.
3. heredoc and nowdoc
3.1 heredoc
Heredoc can be understood as defining strings without double quotation marks, but the effect is the same as using double quotation marks. [Double quotation marks are equivalent to common characters]
Syntax requirements:
1. Start and end tags use the same string, which is usually written with uppercase letters.
2. do not contain spaces or extra characters after the start mark.
3. The ending mark must be written at the top, with no indentation or space, and a semicolon at the end of the ending mark.
4. Variables between the start tag and the end tag can be parsed normally, but functions cannot.
Eg.
<? Php $ var = 'this is suifeng'; echo <"EOT" <! DOCTYPE html>
From the printed results, we can see that double quotation marks are only a common character. The other effects are the same as the strings in double quotation marks.
3.2. nowdoc
Similarly, nowdoc does not use single quotes, but the effect is equivalent to a string defined using single quotes. [Single quotes are equivalent to common characters]
Syntax: Same as heredoc
Eg.
<? Php $ var = 'this is suifeng'; echo <'eot' <! DOCTYPE html>
Note:
The delimiters must not contain any characters after the delimiters, especially space characters. Otherwise, errors may occur.
In the preceding example, if there is a space after the EOT, this error will be reported: [Parse error: syntax error, unexpected '<' (T_SL )]
Summary
The above is a detailed explanation of the four methods for defining PHP Strings. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!