This article mainly introduces the four ways to define strings in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
1. Single quotes
In single quotes, any special characters will be output as they are [except \\ and \' will be escaped and output] only Only single quotes, backslashes are escaped
##2, double quotes
In double quotes, the variable will be parsed when $ is encountered;
#Backslashes are escaped in double quotes Escape characters for bar;
##The difference between single and double quotes # Double quotes will replace the value of the variable, while single quotes will output it as a string. Support for escapingPerformance and speed issues, since the string in double quotes needs to detect whether it contains a variable modified with the $ symbol, so in theory, single quotes are faster (Single quotes are more efficient)
3. heredoc heredoc can be understood as not using double quotes Quotes are used to define a string, but the effect is the same as using double quotes. [Double quotes are equivalent to ordinary characters at this time] (long string) Grammar requirements : 1. The start tag and the end tag use the same string, usually written in uppercase letters. 2. No spaces or extra characters can appear after the opening tag. 3. The closing tag must be written at the beginning, without indentation or spaces, and there must be a semicolon at the end of the closing tag. 4. Variables between the start tag and the end tag can be parsed normally, but functions cannot.
At this time, the double quotation mark is just an ordinary character; other effects are the same as those of the double quotation mark string Example: ##Output: This is suifeng ## 666666 ##4、 Similarly, nowdoc does not use single Quotes are used to define a string, but the effect is equivalent to using single quotes to define a string. [Single quotes are equivalent to ordinary characters at this time]
Three major data types [scalar, consistent, special] Floating point type Floating point type cannot be used in comparison operators Boolean type Seven cases of false Integer 0, floating point 0.0, Boolean false, empty string, 0 string, empty array, null Array type Super global array
nullThree cases Direct assignment to null, undefined variables, variables destroyed by unset() Constant Definition const define const is faster, It is a language structure, define is a function define cannot be used to define class constants, const can. Once a constant is defined, it cannot be modified or deleted Predefined constants (magic constants)
__LINE__ : Returns the current line number in the file. It can also be written as __line__. __FILE__: Returns the absolute path of the current file (including the file name). ##__DIR__: Returns the absolute path of the current file (excluding the file name), equivalent to dirname( __FILE__). __FUNCTION__: Returns the name of the current function (or method). __CLASS__: Returns the current class name (including the scope or namespace of the class). __TRAIT__: Returns the current trait name (including the trait's scope or namespace). __METHOD__: Returns the current method name (including class name). __NAMESPACE__: Returns the name of the namespace of the current file. How to define an empty object in PHP <?php
$var = 'this is suifeng';
echo <<<"ETO"
$var <br/>
"666666"
ETO
##data Type
Related recommendations:
#
The above is the detailed content of Four ways to define strings in PHP. For more information, please follow other related articles on the PHP Chinese website!