|
A string is a collection of characters that is treated as a single entity. In PHP, strings are enclosed in quotation marks, and you can declare a string type variable by assigning it a string that is contained in either single or double quotes. The following examples are identical; both create a variable called $phrase that contains the phrase shown: $phrase = "The sky is falling"; $phrase = 'The sky is falling';
Escaping Characters with BackslashDouble quotes can be used within single-quoted strings and vice versa. For instance, these string assignments are both valid: $phrase = "It's time to party!"; $phrase = 'So I said, "OK"'; However, if you want to use the same character within a quoted string, you must escape that quote by using a backslash. The following examples demonstrate this: $phrase = 'It\'s time to party!"; $phrase = "So I said, \"OK\""; In the previous examples, if the backslash were not used, PHP would mismatch the quotes, and an error would result. Which style of quoting you use largely depends on personal preference and, hopefully, a desire to create tidy code. Remember, though, as you saw in Lesson 2, "Variables," that a variable prefixed with a dollar sign inside a double-quoted string is replaced with its values, whereas in a single-quoted string, the dollar sign and variable name appear verbatim. If you want a dollar sign to form part of a double-quoted string, you can also escape this by using a backslash. For example, the following two statements are equivalent: $offer = 'Save $10 on first purchase'; $offer = "Save \$10 on first purchase"; Without the backslash, the second example would attempt to find the value of a variable called $10, which is, in fact, an illegal variable name. The backslash character can also be used in a double-quoted string to indicate some special values inside strings. When followed by a three-digit number, it indicates the ASCII character with that octal value. You can send the common nonprintable ASCII characters by using standard escape characters. A newline is \n, tab is \t, and so on. Refer to man ascii on your system or www.ascii.cl for a comprehensive list. ConcatenationYou have already seen how strings can be joined using the period symbol as a concatenation operator. A compound version of this operator, .=, can be used to append a string to an existing variable. The following example builds up a string in stages and then displays the result: $phrase = "I want "; $phrase .= "to teach "; $phrase .= "the world "; $phrase .= "to sing"; echo $phrase; The phrase appears as expected. Note the use of spaces after teach and world to ensure that the final string is correctly spaced. Comparing StringsYou can compare string values simply by using the standard comparison operators. To check whether two strings are equal, you use the double equals (==) sign: if ($password == "letmein") echo "You have a guessable password"; The equality operator, when applied to strings, performs a case-sensitive comparison. In the previous example, any other capitalization of $password, such as LetMeIn, would not pass this test. The inequality operators<, <=, >, and >=perform a comparison based on the ASCII values of the individual characters in the strings. The following condition could be used to divide people into two groups, based on their last namethose with names beginning AM and those beginning NZ: if ($last_name < "N") echo "You are in group 1"; else echo "You are in group 2";
|
|