1.0. IntroductionStrings 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.
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
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.
Example 1-2 shows some double-quoted strings. Double-quoted strings
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
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
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
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
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
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. |