Sunday, September 17, 2017

PHP String


A string is series of characters, where a character is the same as a byte. PHP only supports a 256-character set, and hence does not offer native Unicode support.

A string literal can be specified in four different ways:

  1. single quoted
  2. double quoted
  3. heredoc syntax
  4. nowdoc syntax (since PHP 5.3.0)


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, double it (\\). 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.

<?php
echo 'this is a simple string';

echo 'You can also have embedded newlines in 
strings this way as it is
okay to do'."<br>";

// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"'."<br>";

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?'."<br>";

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?'."<br>";

// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline'."<br>";

// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either'."<br>";
?>
Output:
---------------------
this is a simple stringYou can also have embedded newlines in strings this way as it is okay to do
Arnold once said: "I'll be back"
You deleted C:\*.*?
You deleted C:\*.*?
This will not expand: \n a newline
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:
  1. \n linefeed (LF or 0x0A (10) in ASCII)
  2. \r carriage return (CR or 0x0D (13) in ASCII)
  3. \t horizontal tab (HT or 0x09 (9) in ASCII)
  4. \v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
  5. \e escape (ESC or 0x1B (27) in ASCII) (since PHP 5.4.4)
  6. \f form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
  7. \\ backslash
  8. \$ dollar sign
  9. \" double-quote
<?php
echo "this is a simple string";

echo "You can also have embedded newlines in 
strings this way as it is
okay to do"."<br>";

// php allow escape sequences charcters
echo "Arnold \ronce said:\t I ll be back"."\n";

?>
Output:
------------------
this is a simple stringYou can also have embedded newlines in strings this way as it is okay to do
Arnold once said: I ll be back


Heredoc:

A third way to delimit strings 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.
<?php
echo <<<"FOOBAR"
Hello World!
FOOBAR;
?>
Output:
------------------
Hello World!


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. 

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.

<?php
class foo {
    public $bar = <<<'EOT'
bar
EOT;
}
?>


No comments:

Post a Comment