Section 1.0. Introduction


1.0. Introduction

Strings in PHP are sequences of bytes, such as "We hold these truths to be self-evident" or "Once upon a time" or even "111211211." When you read data from a file or output it to a web browser, your data are represented as strings.

PHP strings are binary-safe (i.e., they can contain null bytes) and can grow and shrink on demand. Their size is limited only by the amount of memory that is available to PHP.

Usually, PHP strings are ASCII strings. You must do extra work to handle non-ASCII data like UTF-8 or other multibyte character encodings, see Chapter 19.


Similar in form and behavior to Perl and the Unix shell, strings can be initialized in three ways: with single quotes, with double quotes , and with the "here document" (heredoc) format. With single-quoted strings, the only special characters you need to escape inside a string are backslash and the single quote itself. Example 1-1 shows four single-quoted strings.

Single-quoted strings

print 'I have gone to the store.'; print 'I\'ve gone to the store.'; print 'Would you pay $1.75 for 8 ounces of tap water?'; print 'In double-quoted strings, newline is represented by \n';

Example 1-1 prints:

I have gone to the store. I've gone to the store. Would you pay $1.75 for 8 ounces of tap water? In double-quoted strings, newline is represented by \n

Because PHP doesn't check for variable interpolation or almost any escape sequences in single-quoted strings, defining strings this way is straightforward and fast.

Double-quoted strings don't recognize escaped single quotes, but they do recognize interpolated variables and the escape sequences shown in Table 1-1.

Table 1-1. Double-quoted string escape sequences

Escape sequence

Character

\n

Newline (ASCII 10)

\r

Carriage return (ASCII 13)

\t

Tab (ASCII 9)

\\

Backslash

\$

Dollar sign

\"

Double quotes

\0 through \777

Octal value

\x0 tHRough \xFF

Hex value


Example 1-2 shows some double-quoted strings.

Double-quoted strings

print "I've gone to the store."; print "The sauce cost \$10.25."; $cost = '$10.25'; print "The sauce cost $cost."; print "The sauce cost \$\061\060.\x32\x35."; 

Example 1-2 prints:

I've gone to the store. The sauce cost $10.25. The sauce cost $10.25. The sauce cost $10.25.

The last line of Example 1-2 prints the price of sauce correctly because the character 1 is ASCII code 49 decimal and 061 octal. Character 0 is ASCII 48 decimal and 060 octal; 2 is ASCII 50 decimal and 32 hex; and 5 is ASCII 53 decimal and 35 hex.

Heredoc -specified strings recognize all the interpolations and escapes of double-quoted strings, but they don't require double quotes to be escaped. Heredocs start with <<< and a token. That token (with no leading or trailing whitespace), followed by semicolon a to end the statement (if necessary), ends the heredoc. Example 1-3 shows how to define a heredoc.

Defining a here document

print <<< END It's funny when signs say things like:    Original "Root" Beer    "Free" Gift    Shoes cleaned while "you" wait or have other misquoted words. END; 

Example 1-3 prints:

It's funny when signs say things like:    Original "Root" Beer    "Free" Gift    Shoes cleaned while "you" wait or have other misquoted words.

Newlines, spacing, and quotes are all preserved in a heredoc. By convention, the end-of-string identifier is usually all caps, and it is case sensitive. Example 1-4 shows two more valid heredocs.

More here documents

print <<< PARSLEY It's easy to grow fresh: Parsley Chives on your windowsill PARSLEY; print <<< DOGS If you like pets, yell out: DOGS AND CATS ARE GREAT! DOGS;

Heredocs are especially useful for printing out HTML with interpolated variables, since you don't have to escape the double quotes that appear in the HTML elements. Example 1-5 uses a heredoc to print HTML.

Printing HTML with a here document

if ($remaining_cards > 0) {     $url = '/deal.php';     $text = 'Deal More Cards'; } else {     $url = '/new-game.php';     $text = 'Start a New Game'; } print <<< HTML There are <b>$remaining_cards</b> left. <p> <a href="$url">$text</a> HTML;

In Example 1-5, the semicolon needs to go after the end-of-string delimiter to tell PHP the statement is ended. In some cases, however, you shouldn't use the semicolon. One of these cases is shown in Example 1-6, which uses a heredoc with the string concatenation operator .

Concatenation with a here document

$html = <<< END <div > <ul > <li> END . $listItem . '</li></div>'; print $html;

Assuming some reasonable values for the $divClass, $ulClass, and $listItem variables, Example 1-6 prints:

<div > <ul > <li> The List Item </li></div>

In Example 1-6, the expression needs to continue on the next line, so you don't use a semicolon. Note also that in order for PHP to recognize the end-of-string delimiter, the . string concatenation operator needs to go on a separate line from the end-of-string delimiter.

Individual bytes in strings can be referenced with square brackets. The first byte in the string is at index 0. Example 1-7 grabs one byte from a string.

Getting an individual byte in a string

$neighbor = 'Hilda'; print $neighbor[3];

Example 1-7 prints:

d

You can also use curly braces to access individual byte in a string. That is, $neighbor{3} is the same as $neighbor[3]. The curly brace syntax is a newer addition to PHP. It provides a visual distinction between string indexing and array indexing.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net