Shares

PHP: Do you know your string types?

Have you ever wondered what the difference is between a single-quoted and double-quoted string when writing a PHP script? Many beginner PHP coders make the mistake of thinking they’re interchangeable, but there is an important difference! Plus, there are two other types of strings you should know about. We’ll discuss all that and more in this article.

To begin with, let’s summarise the four basic types of strings that may be used within a PHP script. Those are:

  • single-quoted,
  • double-quoted,
  • heredoc syntax,
  • and nowdoc syntax.

You’re probably familiar with the first two if you’ve done any amount of PHP coding in the past, but the second two may be unfamiliar to you. So, let’s start with the easiest ones, and then move on to the more advanced string types.

Single-Quoted

The single-quoted string is a string of text contained between two single quotes. For example, if I wanted to define $x using a single-quoted string, I’d write:

 $x = 'Hello, World!';

Notice that surrounding the value, there is a single quotation mark on each end. If you wanted to actually contain a single quote or a backslash within the quote, you must use the escape character before it, which is the backslash. For example:

$x = 'Jeremy\'s a nice guy.';
echo $x;
// outputs: Jeremy's a nice guy.

As you can see, The single quote within the quote was preceded by the escape character (backslash), and therefore became part of the string. If you wanted to contain a literal backslash in the quote, you would need to put two backslashes. For example:

$x = 'The file is contained in C:\\drivers\\hp15897.dll';
echo $x;
// outputs: The file is contained in C:\drivers\hp15897.dll

The most interesting thing about the single-quoted string, however, is that it is much more literal than the double-quoted string. For example, take the following code:

$x = 'World';
$y = 'Hello, $x!';
echo $y;
// outputs: Hello, $x!

Hm, maybe you were expecting to output “Hello, World!”? No, the single-quoted string does not accept variables within it. The dollar sign is treated literally, just like any other character. Therefore, if you wanted to include a variable within a single quote, you would have to concatenate it between two quotes.

Double-Quoted

The next type of string is the double-quoted string. In many ways it is similar to the single-quoted string, except for two important differences. Firstly, you can use a single quote within the double quote without using an escape character, however, to use a double quote within a double-quoted string, you must use the escape character (backslash). Secondly, the double-quoted string is interpreted, meaning that variables are treated as variables.

Let us illustrate these particularities through two examples.

Example 1:
$x = "My dog's name is \"Buddy.\"";
echo $x;
// outputs: My dog's name is "Buddy."
Example 2:
$x = "World";
$y = "Hello, $x!";
echo $y;
// outputs: Hello, World!

In the first example, the single quote is used without an escape character, and the two double quotes have backslashes in front of them. In the second example, you can see the in the $y variable, $x is actually interpreted as a variable.

Now that you understand the purpose of single- and double-quoted strings, let’s move on to heredoc and nowdoc. These two types of lesser-known strings can come in very handy!  The key thing to remember is that heredoc is equivalent to double-quoted, while nowdoc is equivalent to single-quoted. The main difference is that these two string types don’t need nearly as many escape characters, since they are not contained within quotation marks. This is particularly useful for large blocks of code, for example, all the HTML for a certain web page or at least a certain portion of a web page. Personally I often use heredoc and nowdoc for containing things such as headers and footers that are repeated on every page of a website.

With heredoc and nowdoc, instead of using quotation marks, you define your own code word for signifying the end of the string. Scripters often use the code “EOT” (short for End Of Text) or “EOD” (for End Of Doc) just for consistency, but it’s really up to you. The important thing is that you use a code that probably won’t appear anywhere in the text being quoted.

Heredoc

First, with heredoc, remember that just like a double-quoted string, variables are interpreted as variables. The opening codeword is preceded by three “lesser-than” symbals. Also, the codeword signifying the end of a quote must be alone on its line. This means it must have NO indentation and must be followed immediately by a carriage return, except for an optional semicolon. For example:

$title = "Hello!";
$header = <<
 
EOT;
 
$echo $header;
// Outputs:
//
//
//

As you can see from this example, the double quotes are interpreted literally, while the variable is treated as a real variable.

Nowdoc

The final type of string is the nowdoc. Think of this as being in the format of the heredoc, except with the rules of a single-quoted string. The nowdoc is identified by enclosing the code word in single quotes. The advantage of a nowdoc is that it is always treated entirely as plan text, so there’s no worry of accidentally including a special character that triggers some undesired action. Here’s an example:

$title = "Hello!";
$text = "My world!";
$foo = array('bar', 'one', 'two', 'three');
$content = <<$text {$foo[0]} contains 'bar'.
 
EOT;
// outputs:
//
//
//
////// $text // {$foo[0]} contains 'bar'. //
//

As you can see, all of my variables and other text were taken absolute literally! This might make things more difficult in some situation, but in others, this is ideal. For example, if if I wanted to store this entire tutorial in a variable, I wouldn’t want to risk any of my examples being interpreted as actual PHP code, so I might enclose the whole thing in a nowdoc quote. Again, just as with heredoc, it’s very important that your closing keyword (in this case EOT;) have ZERO indentation, and that there be a line break immediately afterward, with no further characters on the same line.

I hope this helps a few of you better understand PHP! This is one more tool to have in your scripter’s toolbox which will enhance your ability to write a variety of different scripts.

About Jeremy Andrews

Freelance PHP/MySQL developer, tech support guy, travel agent, transit photographer, and lover of poutine. Jeremy Andrews is a young guy from Montreal, Canada who enjoys writing about technology, can develop a website, and can do your travel arrangements! Contact him today at jeremy@jerail.ca for more info on any of these services.