Programming PHPProgramming PHPSearch this book

Chapter 4. Strings

Most data you encounter as you program will be sequences of characters, or strings. Strings hold people's names, passwords, addresses, credit-card numbers, photographs, purchase histories, and more. For that reason, PHP has an extensive selection of functions for working with strings.

This chapter shows the many ways to write strings in your programs, including the sometimes-tricky subject of interpolation (placing a variable's value into a string), then covers the many functions for changing, quoting, and searching strings. By the end of this chapter, you'll be a string-handling expert.

4.1. Quoting String Constants

There are three ways to write a literal string in your program: using single quotes, double quotes, and the here document (heredoc) format derived from the Unix shell. These methods differ in whether they recognize special escape sequences that let you encode other characters or interpolate variables.

The general rule is to use the least powerful quoting mechanism necessary. In practice, this means that you should use single-quoted strings unless you need to include escape sequences or interpolate variables, in which case you should use double-quoted strings. If you want a string that spans many lines, use a heredoc.

4.1.4. Here Documents

You can easily put multiline strings into your program with a heredoc, as follows:

$clerihew = <<< End_Of_Quote
Sir Humphrey Davy
Abominated gravy.
He lived in the odium
Of having discovered sodium.
End_Of_Quote;
echo $clerihew;
Sir Humphrey Davy
Abominated gravy.
He lived in the odium
Of having discovered sodium.

The <<< Identifier tells the PHP parser that you're writing a heredoc. There must be a space after the <<< and before the identifier. You get to pick the identifier. The next line starts the text being quoted by the heredoc, which continues until it reaches a line that consists of nothing but the identifier.

As a special case, you can put a semicolon after the terminating identifier to end the statement, as shown in the previous code. If you are using a heredoc in a more complex expression, you need to continue the expression on the next line, as shown here:

printf(<<< Template
%s is %d years old.
Template
, "Fred", 35);

Single and double quotes in a heredoc are passed through:

$dialogue = <<< No_More
"It's not going to happen!" she fumed.
He raised an eyebrow.  "Want to bet?"
No_More;
echo $dialogue;
"It's not going to happen!" she fumed.
He raised an eyebrow.  "Want to bet?"

Whitespace in a heredoc is also preserved:

$ws = <<< Enough
  boo
  hoo

Enough;
// $ws = "  boo\n  hoo\n";

The newline before the trailing terminator is removed, so these two assignments are identical:

$s = 'Foo';
// same as
$s = <<< End_of_pointless_heredoc
Foo
End_of_pointless_heredoc;

If you want a newline to end your heredoc-quoted string, you'll need to add an extra one yourself:

$s = <<< End
Foo

End;


Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.