Tuesday, January 22, 2013



A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode. See utf8_encode() and utf8_decode() for some Unicode support.



A string literal can be specified in three different ways.
  • single quoted
  • double quoted
  • heredoc syntax

Single quoted

The easiest way to specify a simple string is to enclose it in single quotes (the character ').
To specify a literal single quote, you will need to escape it with a backslash (\), like in many other languages. If a backslash needs to occur before a single quote or at the end of the string, you need to double it. Note that if you try to escape any other character, the backslash will also be printed! So usually there is no need to escape the backslash itself.


<?phpecho 'this is a simple string';

'You can also have embedded newlines in
strings this way as it is
okay to do'

 // Outputs: Arnold once said: "I'll be back"
 echo 'Arnold once said: "I\'ll be back"'; 
// Outputs: You deleted C:\*.*? 
echo 'You deleted C:\\*.*?'; 
// Outputs: You deleted C:\*.*?
 echo 'You deleted C:\*.*?'; 
// 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 understands more escape sequences for special characters:


Another way to delimit strings is by using heredoc syntax ("<<<"). One should provide an identifier after <<<, then the string, and then the same identifier to close the quotation.
The closing identifier must begin in the first column of the line. Also, the identifier used 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.


No comments:

Post a Comment