Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 5 ■ WHAT'S NEW IN PHP 6^45

Listing 5-4. Unicode String Length

You will notice that the Unicode characters actually form a string with a length equal to 5.
When Unicode semantics are turned off, the Unicode string is reported as having a length of 15.
Even with unicode.semantics off, the $unicode string is still treated as Unicode in PHP 6,
and your output will look correct.
It is possible to force binary handling of string data, even if the source file contents are
Unicode and Unicode semantics are enabled. Listing 5-5 shows the same Unicode string as in
Listing 5-4, but using the new binary string literal format.

Listing 5-5. Binary String Literals

Notice that the output is assuming that the input string is in binary, because of the b before
the quotation marks. The output appears distorted on the screen because the encoding for binary
strings uses the value of the unicode.runtime_encoding setting in the php.ini file—in this case,
ISO-8859-1.
You can cast between binary and Unicode just as you cast between types, as demonstrated
in Listing 5-6.

McArthur_819-9C05.fm Page 45 Wednesday, February 27, 2008 8:38 AM

Free download pdf