ONE NET WIKI Free and professional IT Wiki

Four ways to specify a string in PHP

2018-09-01 ONE NET WIKI

string.jpg

A string literal can be specified in four different ways: single quoted, double quoted, heredoc syntax and nowdoc syntax.


Single quoted

The simplest way to specify a string is to enclose it in single quotes (the character ').

echo 'this is a simple string';
  • To specify a literal single quote, escape it with a backslash (\).

// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
  • To specify a literal backslash, double it (\\).

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
  • All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.

// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';

Double quoted

If the string is enclosed in double-quotes ("), PHP will interpret the following escape sequences for special characters:

SequenceMeaning
\nlinefeed (LF or 0x0A (10) in ASCII)
\rcarriage return (CR or 0x0D (13) in ASCII)
\thorizontal tab (HT or 0x09 (9) in ASCII)
\vvertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\eescape (ESC or 0x1B (27) in ASCII) (since PHP 5.4.4)
\fform feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\backslash
\$dollar sign
\"double-quote
\[0-7]{1,3}the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. "\400" === "\000")
\x[0-9A-Fa-f]{1,2}the sequence of characters matching the regular expression is a character in hexadecimal notation
\u{[0-9A-Fa-f]+}the sequence of characters matching the regular expression is a Unicode codepoint, which will be output to the string as that codepoint's UTF-8 representation (added in PHP 7.0.0)

The most important feature of double-quoted strings is the fact that variable names will be expanded.


maxresdefault.jpg

Heredoc (<<<)

Heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

The closing identifier must begin in the first column of the line.

Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
?>
<?php
// Identifier must not be indented
class foo {
    public $bar = <<<EOT
bar
EOT;
}
?>

Quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded.


tout-sur-php-73-et-ses-rfc-7-638.jpg

Nowdoc

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings.

A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc.

The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML <![CDATA[ ]]> construct, in that it declares a block of text which is not for parsing.

<?php
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
?>
Editor's Note: All the articles in this website are original. If reproduced, please indicate the source and link.
353
0
All Comments (0)
Expand to publish a comment

Web Development


Web Management


Web Marketing


Web Security