Core PHP

Lesson 3: String Literals

Overview

This lesson describes the four string literals that are used in PHP. These four literals give us different methods for representing strings of characters in a program for the sake of convenience. Some are better for displaying variable names. Some are better for variable values. Finally, some are better for creating strings with quotations inside them.

The Four Types of Literals

A literal is a representation in code of a data type. In PHP, there are four types of string literals: single quote, double quote, nowdoc, and heredoc. Each of these is used in the program below. The single quote string is enclosed in single quotation marks. The double quote string is enclosed in double quotation marks. The nowdoc and heredoc strings are enclosed in delimeters that are defined by the programmer. Here, I have used NOW_DOC and HERE_DOC for clarity.

Both the nowdoc and heredoc begin with a triple less than signs: <<<. However, the first delimeter of the nowdoc is enclosed in single quotes to distinguish it from the heredoc. Additionally, both terminated by the second delimeter placed on its own line.

Prog1.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php

$sSingleQuotes = 'Single Quotes';

$sDoubleQuotes = "Double Quotes";

$sNowDoc =<<<'NOW_DOC'
Nowdoc
NOW_DOC;

$sHereDoc =<<<HERE_DOC
Heredoc
HERE_DOC;

echo "<pre>";
echo $sSingleQuotes;
echo "</pre>";

echo "<pre>";
echo $sDoubleQuotes;
echo "</pre>";

echo "<pre>";
echo $sNowDoc;
echo "</pre>";

echo "<pre>";
echo $sHereDoc;
echo "</pre>";

?>
  </body>
</html>
 
 

Output for Prog1.php

 

Variables in Strings

One of the primary differences between the different string literals is how they display variables. When a variable name is included in a single quote string or a nowdoc, the name of the variable is printed. However, when a variable name is included in a double quote string or a heredoc, the value of the variable is printed. In this way, the single quote string and the nowdoc are similar to each other. Likewise, the double quote string and the heredoc are similar. This is illustrated in the program below.

Prog2.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php

$iNinetyOne = 91;

$sSingleQuotes = '$iNinetyOne';

$sDoubleQuotes = "$iNinetyOne";

$sNowDoc =<<<'NOW_DOC'
$iNinetyOne
NOW_DOC;

$sHereDoc =<<<HERE_DOC
$iNinetyOne
HERE_DOC;

echo "<pre>";
echo $sSingleQuotes;
echo "</pre>";

echo "<pre>";
echo $sDoubleQuotes;
echo "</pre>";

echo "<pre>";
echo $sNowDoc;
echo "</pre>";

echo "<pre>";
echo $sHereDoc;
echo "</pre>";

?>
  </body>
</html>
 
 

Output for Prog2.php

 

Using Quotations in Strings

The main problem that the nowdoc and heredoc help to solve is the representation of strings with quotations in them. Since the single and double quote strings use quotation makes as their delimeters, they need to use a slash before their respective delimters when they are included in a string. A slash before a character like this is called an escape sequence. Nowdocs and heredocs do not require escape sequences because they do not have quotation marks as their delimiters. Below, the four strings designate the same string sequence, but the nowdoc and heredoc do not require escape sequences.

Prog3.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$sSingleQuotes = 'At this the Jews there began to grumble about him because he said, "I am the
bread that came down from heaven." They said, "Is this not Jesus, the son of Joseph, whose
father and mother we know? How can he now say, \'I came down from heaven\'?"';

$sDoubleQuotes = "At this the Jews there began to grumble about him because he said, \"I am the
bread that came down from heaven.\" They said, \"Is this not Jesus, the son of Joseph, whose
father and mother we know? How can he now say, 'I came down from heaven'?\"";

$sNowDoc =<<<'NOW_DOC'
At this the Jews there began to grumble about him because he said, "I am the bread that came down
from heaven." They said, "Is this not Jesus, the son of Joseph, whose father and mother we know?
How can he now say, 'I came down from heaven'?"
NOW_DOC;

$sHereDoc =<<<HERE_DOC
At this the Jews there began to grumble about him because he said, "I am the bread that came down
from heaven." They said, "Is this not Jesus, the son of Joseph, whose father and mother we know?
How can he now say, 'I came down from heaven'?"
HERE_DOC;

echo "<p>$sSingleQuotes</p>";
echo "<p>$sDoubleQuotes</p>";
echo "<p>$sNowDoc</p>";
echo "<p>$sHereDoc</p>";
?>
  </body>
</html>
 
 

Output for Prog3.php

 

Escape Sequences

For escape sequences, single quote strings and nowdocs are similar as are double quote strings and heredocs. For single quote strings, there are only two escape sequences: \' \. These escape sequences are not needed for nowdocs; so they have none. Double quote strings have a number of escape sequences, but I will just give the most commonly used ones: $ \" \. These escapes sequences generate a newline character, a dollar sign, a tab, a double quotation mark, and a slash, respectively. All of these sequences are available in heredocs, except the sequence for the double quotation mark, because it is not needed. This is illustrated in the program below.

Prog4.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$sSingleQuotes = 'Escape Sequences: \' \\';

$sDoubleQuotes = "Escape Sequences: \n \$ \t \" \\";

$sNowDoc =<<<'NOW_DOC'
Escape Sequences: \' \\
NOW_DOC;

$sHereDoc =<<<HERE_DOC
Escape Sequences: \n \$ \t \" \\
HERE_DOC;

echo "<pre>";
echo $sSingleQuotes;
echo "</pre>";

echo "<pre>";
echo $sDoubleQuotes;
echo "</pre>";

echo "<pre>";
echo $sNowDoc;
echo "</pre>";

echo "<pre>";
echo $sHereDoc;
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog4.php

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.