In php, what are the representations of strings?

下次还敢
Release: 2024-04-26 08:42:14
Original
712 people have browsed it

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.

In php, what are the representations of strings?

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login
.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!