Team LiB
Previous Section Next Section

Strings in PHP

Like most other languages, PHP defines a string as a sequence of characters. It's important to understand that the concept of "character" is not limited to symbols people normally use in their day-to-day life, like the letters of the alphabet, digits, and punctuation marks. The meaning associated with the term "character" simply indicates a single byte of data. Depending on how this byte is used, it could mean a letter, the pixel of an image, or even part of a song encoded in MP3 format.

Because a character is intended as a single byte of data, the native PHP string functions are capable of handling only up to 256 values for each character. Some languages, such as Chinese and Japanese, for example, have far more than 256 characters; therefore, they cannot be properly represented using normal strings. Luckily, PHP provides a set of multibyte string functions (MBString) that can deal with these languages using special variables.

Speed and Efficiency of String Expressions

The three string definition notations that we examined so far provide different levels of performance.

Although it's unlikely that your application will suffer performance degradation because of your string expressionsit's much more likely that you'll have to deal with other, bigger problems firstit's good to know that the fastest way to declare a string is to use single quotation marks, because the interpreter does not have to scan the string to perform substitutions (excluding \' and \\).

The double-quote syntax is slower because the entire string expression has to be scanned and substitutions have to be made for the variable expressions in it. Finally, the heredoc syntax is the slowest, because on top of the scanning operations to detect substitutions and special characters, the interpreter also has to worry about finding your delimiter.

    Team LiB
    Previous Section Next Section