A PHP string can be specified in four different ways:
- single quoted
The simplest way to specify a string is to enclose it in single quotes (the character ').
To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash before a single quote, or at the end of the string , double it (\\). Note that attempting to escape any other character will print the backslash too.
- double quoted
If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters.
As in single quoted string s, escaping any other character will result in the backslash being printed too. Before PHP 5.1.1, the backslash in \{$var} had not been printed.
The most important feature of double-quoted string s is the fact that variable names will be expanded. See string parsing for details.
- heredoc syntax
A third way to delimit string s is the 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.
- nowdoc syntax (since PHP 5.3.0)
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 construct, in that it declares a block of text which is not for parsing.
A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.
Tuesday, 1 June 2010
PHP Strings
What is a string?
To define a string you should have a series of character such that a character is the same as a byte. There are 256 different characters possible. Because of this the PHP does not have native support of Unicode. For some basic Unicode functionality see the utf8_encode() and utf8_decode().
To define a string you should have a series of character such that a character is the same as a byte. There are 256 different characters possible. Because of this the PHP does not have native support of Unicode. For some basic Unicode functionality see the utf8_encode() and utf8_decode().
Subscribe to:
Posts (Atom)