05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Table 4-4. Single-character escapes recognized by addcslashes( ) and stripcslashes( ) (continued)<br />

ASCII value Encoding<br />

10 \n<br />

11 \v<br />

12 \f<br />

13 \r<br />

Call addcslashes( ) with two arguments—the string to encode and the characters to<br />

escape:<br />

$escaped = addcslashes(string, charset);<br />

Specify a range of characters to escape with the ".." construct:<br />

echo addcslashes("hello\tworld\n", "\x00..\x1fz..\xff");<br />

hello\tworld\n<br />

Beware of specifying '0', 'a', 'b', 'f', 'n', 'r', 't', or'v' in the character set, as<br />

they will be turned into '\0', '\a', etc. These escapes are recognized by C and <strong>PHP</strong><br />

and may cause confusion.<br />

stripcslashes( ) takes a string and returns a copy with the escapes expanded:<br />

$string = stripcslashes(escaped);<br />

For example:<br />

$string = stripcslashes('hello\tworld\n');<br />

// $string is "hello\tworld\n"<br />

Comparing Strings<br />

<strong>PHP</strong> has two operators and six functions for comparing strings to each other.<br />

Exact Comparisons<br />

You can compare two strings for equality with the == and === operators. These operators<br />

differ in how they deal with non-string operands. The == operator casts nonstring<br />

operands to strings, so it reports that 3 and "3" are equal. The === operator<br />

does not cast, and returns false if the types of the arguments differ.<br />

$o1 = 3;<br />

$o2 = "3";<br />

if ($o1 == $o2) {<br />

echo("== returns true");<br />

}<br />

if ($o1 === $o2) {<br />

echo("=== returns true");<br />

}<br />

== returns true<br />

86 | Chapter 4: Strings<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!