Datatypes in PHP - Strings : Tutorial




4. String data type

In PHP strings are series of characters.
PHP only supports a 256-character set.
PHP does not support Unicodes
PHP string can be as large as 2GB.
There are 4 types of Strings in PHP:
1. Single quoted
2. Double quoted
3. Heredoc syntax
4. Nowdoc syntax (since PHP 5.3.0)

Single quoted Strings in PHP

Single quoted strings are enclosed with ' (single quote character)
Variables and escape sequences for special characters are not expanded in Single quoted strings.
If we have a single quote within the string we escape it using /
If there is a \ (forward-slash) it can be escaped by // (double slash)
Escape sequences like \r \n are not expanded in Single quotes strings.

Examples :
<?php

$var = "Mike";

echo 'Hello, <br>';

echo '"Double quotes here have no issues."<br>';

echo 'We can
      have 
	  multi-line
      Single quote strings.<br>';

echo 'Newline sequence \n
      wont expand.<br>';

echo 'I\'m here!<br>';

echo 'My Name is $var, err!!, variables won\'t expand.<br>';

?>
Output

Hello, "Double quotes here have no issues." We can have multi-line Single quote strings. Newline sequence \n wont expand. I'm here! My Name is $var, err!!, variables won't expand.

Double quoted Strings in PHP

Double quoted String literals are enclosed with double quote characters (").

e.g. : $var = "Double quoted String";

Escape sequences are expanded in Double quoted Strings
Variable names are expanded in Double quoted Strings

Double quoted Strings interprets more escape sequences then Single quoted strings.

\n - Newline or Linefeed
\r - Carriage return
\t - Horizontal tab
\v - Vertical tab
\e - Escape
\f - Form feed
\\ - Backslash
\$ - Dollar sign
\" - Double-quote

<?php

$var = "Mike";

echo "Hello, <br>";

echo "'Single quotes here have no issues.'<br>";

echo "We can
      have 
	  multi-line
      Double quote strings.<br>";

echo "Newline sequence \n
      is expand in Double quoted strings.<br>";

echo "He is the \"Man\"!<br>";

echo "My Name is $var, Wow!!, variable names do get 
      expanded in Double quoted strings. <br>";

?>
Output

Hello, 'Single quotes here have no issues.' We can have multi-line Double quote strings. Newline sequence is expand in Double quoted strings. He is the "Man"! My Name is Mike, Wow!!, variable names do get expanded in Double quoted strings.

Heredoc Strings in PHP

Heredoc is just like Double quoted strings, but it does not start and end with a double quotes, hence there is no need to escape double quotes, while we can use other escape codes listed above.

Syntax:
The Heredoc String starts with <<< followed by by and identifier, then a newline, then the string and finally ends with the same identifier and a semicolon.

Example:
<?php

$str = <<<EOD
This is an example
of heredoc spanning
multiple lines.
EOD;

echo $str;

?>
Output

This is an example of heredoc spanning multiple lines.

Notes : Here EOD is used as an Identifier.
The identifier at the end of herdoc (EOD; in above example) should not have and leading or trailing spaces.


Nowdoc Strings in PHP

Nowdoc's are similar to heredoc's, except variables are not expanded in nowdoc's (just like Single quoted Strings)
String identifier is surrounded it single quote.
Only difference between heredoc's and nowdoc's syntax is that nowdoc's starting identifier is surrounded by single quotes.

<?php

$name = "Mike";

//Heredoc example
echo <<<EOT
My name is $name
I love PHP.
EOT;

echo "<br>";
		
//Nowdoc example
echo <<<'EOT'
My name is $name
I love PHP.
EOT;


?>
Output

My name is Mike I love PHP. My name is $name I love PHP.

To sum up, Nowdoc behave just like "Single Quoted Strings" and,
Heredocs behave just like "Double Quoted Strings".