Home >Backend Development >PHP Tutorial >In php, what are the representations of strings?
PHP strings have 5 representations: single quotes ignore escape sequences; double quotes allow escape sequences; Heredoc syntax is used for multi-line strings and retains newlines; Nowdoc syntax is used for multi-line strings But newlines are not preserved; backslash strings provide access to object properties through property names.
PHP string representation
There are 5 representations of strings in PHP:
1. Single quote (')
Single quoted string ignores any escape sequence, including the escape character itself ('). This is the most basic and common string representation.
For example:
<code class="php">$str = 'Hello, world!';</code>
2. Double quotes (")
Double quote strings allow the use of escape sequences. For example, \n represents a newline character, \t represents the tab character. For example:
<code class="php">$str = "Hello, world!\n";</code>3. Heredoc syntax
Heredoc syntax allows the use of multi-line strings, and Line breaks and other whitespace characters are preserved. It starts with <<< and ends with a semicolon (;)
For example:
<code class="php">$str = <<<EOT Hello, world! This is a multi-line string. EOT;</code>4. Nowdoc Syntax
Nowdoc syntax is similar to Heredoc syntax, but it does not preserve newlines and other whitespace characters. It starts with <<< and ends with a single or double quote
. For example:
<code class="php">$str = <<<EOF Hello, world! This is a multi-line string. EOF;</code>5. Backslash string (my_var->{"propname"})
Backslash string is allowed by backslash ( my_var->{"propname"}) to access the properties in the object.
For example:
<code class="php">class MyClass { public $propname = 'Hello, world!'; } $obj = new MyClass(); $str = $obj->{"propname"};</code>.
The above is the detailed content of In php, what are the representations of strings?. For more information, please follow other related articles on the PHP Chinese website!