Chapter 7. Learning PHP String Handling

Although images, sound files, videos, animations, and applets make up an important portion of the World Wide Web, much of the web is still text — one character's worth after another, like this sentence. The basic PHP data type for representing text is the string.

In this chapter, we cover almost all PHP's capabilities for manipulating strings (although we leave more advanced string functions and the pattern-matching power of regular expressions for separate treatment in Chapter 22). We start with the basics of strings, then move to the most commonly used operators and functions.

Strings in PHP

Strings are sequences of characters that can be treated as a unit — assigned to variables, given as input to functions, returned from functions, or sent as output to appear on your user's web page. The simplest way to specify a string in PHP code is to enclose it in quotation marks, whether single quotation marks (') or double quotation marks ("), like this:

$my_string = 'A literal string';
$another_string = "Another string";

The difference between single and double quotation marks lies in how much interpolation PHP does of the characters between the quote signs before creating the string itself. If you enclose a string in single quotation marks, almost no interpolation will be performed; if you enclose it in double quotation marks, PHP will splice in the values of any variables you include, as well as make substitutions ...

Get PHP6 and MySQL® 6 Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.