Definition and Usage
In PHP, a string data type is a non-numeric sequence of charaters.Any character in the ASCII set can be a part of a string. PHP doesn't support UNICODE.
In PHP, literal representation of string can be done with single quotes, double quotes, with heredoc syntax and nowdoc syntax.
Syntax
//Literal assignment of string value to variable $var='Hello World'; //Single quotes $var3="Hello World"; //Double quotes
To embed single quote character inside single quoted string prefix it with '\'. Similarly to embed backslash in single quoted string prefix it with additional backslash. Other escape sequence characters such as \n etc. do not carry any special representation.
Double quoted string treats following escape sequences with their special meaning as follows:
Sequence | Meaning |
---|---|
\n | linefeed |
\r | carriage return |
\t | horizontal tab |
\v | vertical tab (since PHP 5.2.5) |
\e | escape (since PHP 5.4.4) |
\f | form feed (since PHP 5.2.5) |
\\ | backslash |
\$ | dollar sign |
\" | double-quote |
The Heredoc string starts with <<< symbol followed by any identifier of user's choice. From next line, any multiline sequence of characters that may be having any of the above escape sequences. Last line should have same heredoc identifier ending with semicolon.
//Heredoc assignment of string value to variable public $var = <<< XYZ Hello World Welcome to Tutorialspoint XYZ;
Nowdoc strings are similar to heredoc strings. Difference is that identifier must be enclosed in single quotes and escape sequences inside nowdoc string are not parsed and appear as it is.
//Nowdoc assignment of string value to variable public $var = <<< 'XYZ' Hello World Welcome to Tutorialspoint XYZ;
PHP Version
Use of separation symbol "_" is avilable since PHP 7.40
Following example shows single quoted string. The escape sequence \n is not parsed and appears as it is
Example
<?php $var = 'Hello World.\n Welcome to Tutorialspoint'; echo $var; ?>
Output
This will produce following result −
Hello World.\n Welcome to Tutorialspoint
This Example double quoted string. The escape sequence \n is parsed and text appears in two lines
Example
<?php $var = "Hello World.\n Welcome to Tutorialspoint"; echo $var; ?>
Output
This will produce following result −
Hello World. Welcome to Tutorialspoint
This example shows how to use Heredoc and Nowdoc syntax for string representation
Example
<?php //Heredoc $var = <<< STR Hello World Welcome to Tutorialspoint STR; echo $var . "\n"; //Nowdoc $var = <<< 'STR' Hello World Welcome to Tutorialspoint STR; echo $var; ?>
Output
This will produce following result −
Hello World Welcome to Tutorialspoint Hello World Welcome to Tutorialspoint
This Example shows values of a variable is substiuted in heredoc string. Nowdoc string doesn't make substitution
Example
<?php $name = "Mahesh"; $var = <<< STR Hello $name Welcome to Tutorialspoint STR; echo $var . "\n"; //Nowdoc $var = <<<'STR' Hello $name Welcome to Tutorialspoint STR; echo $var; ?>
Output
This will produce following result −
Hello Mahesh Welcome to Tutorialspoint Hello $name Welcome to Tutorialspoint