Section 5.3. Strings


5.3. Strings

You can use {x} notation with strings to read or write individual characters. For example:

     $mystr = "Jello, world?";     $mystr{0} = "H";     $mystr{12} = "!";     print $mystr;

Starting off with a string that doesn't make much sense, we change the first character (position 0) to H, then the twelfth character to an exclamation mark, forming "Hello, world!". As you can see, the first character in a string is numbered 0. That is, a string of length 13, as above, will have its last character at position 12.

5.3.1. Escape Sequences

Escape sequences, the combination of the escape character \ and a letter, are used to signify that the character after the escape character has a special meaning. If you wanted to have the string "And then he said, "That is amazing!", which was true," you would need escape characters because you have double quotes inside double quotes. The valid escape sequences in PHP are shown in Table 5-1.

Table 5-1. Escape sequences and their meanings (continued)

\"

Print the next character as a double quote rather than treating it as a string terminator

\'

Print the next character as a single quote rather than treating it as a string terminator

\n

Print a new line character

\t

Print a tab character

\r

Print a carriage return (used primarily on Windows)

\$

Print the next character as a dollar rather than treating it as part of a variable name

\\

Print the next character as a backslash rather than treating it as an escape character


Here is a code example of these escape sequences in action:

     <?php             $MyString = "This is an \"escaped\" string";             $MySingleString = 'This \'will\' work';             $MyNonVariable = "I have \$zilch in my pocket";             $MyNewline = "This ends with a line return\n";             $MyFile = "c:\\windows\\system32\\myfile.txt";     ?>

Many people forget to escape Windows-style filesystem paths properly, but as you can see, it is simply a matter of adding the appropriate backslashes. If you were to print $MyFile, you would get this:

     c:\windows\system32\myfile.txt 

This is because the escape characters are there to ensure PHP reads the string correctlyPHP reads the \\, understands it to be an escape sequence, so just stores \.

Along the same lines, most escape sequences only work in double-quoted stringsif you type Hello!\n\n\n, PHP will actually print out the characters \n\n\n rather than converting them to new lines. The only escape sequence that works within a single-quoted string is \', which tells PHP that the single quote is not the termination of a string but a literal single quote. It is important to note that escape characters are considered just one character by PHP. They are represented as two in PHP because they cannot physically be typed using your keyboard.

Since the only escape sequence that works in single quotes is \', it is safe to use non-escaped Windows-style filenames in your single-quoted strings, like this:

     <?php             $filename = 'c:\windows\me.txt';             echo $filename;     ?> 



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

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