Page 113 - Beginning PHP 5.3
P. 113

Chapter 5: Strings
                               Within single - quoted strings, you can actually use a couple of escape sequences. Use  \’  to include a lit-
                             eral single quote within a string. If you happen to want to include the literal characters   \’  within a sin-
                             gle - quoted string, use   \\\’    —  that is, an escaped backslash followed by an escaped single quote.


                           By the way, it ’ s easy to specify multi - line strings in PHP. To do this, just insert newlines into the string
                         literal between the quotation marks:

                             $myString = “
                               I stay too long; but here my Father comes:
                               A double blessing is a double grace;
                               Occasion smiles vpon a second leaue
                             “;

                           Including More Complex Expressions within Strings

                           Though you can insert a variable ’ s value in a double - quoted string simply by including the variable ’ s
                         name (preceded by a   $  symbol), at times things get a bit more complicated. Consider the following
                         situation:

                             $favoriteAnimal = “cat”;
                             echo “My favorite animals are $favoriteAnimals”;

                           This code is ambiguous; should PHP insert the value of the  $favoriteAnimal  variable followed by an
                           “s”  character? Or should it insert the value of the (non - existent)  $favoriteAnimals  variable? In fact,
                         PHP attempts to do the latter, resulting in:
                             My favorite animals are

                           Fortunately, you can get around this problem using curly brackets, as follows:

                             $favoriteAnimal = “cat”;
                             echo “My favorite animals are {$favoriteAnimal}s”;

                           This produces the expected result:
                             My favorite animals are cats

                           You can also place the opening curly bracket after the  $  symbol, which has the same effect:


                             echo “My favorite animals are ${favoriteAnimal}s”;
                           The important thing is that you can use the curly brackets to distinguish the variable name from the
                         rest of the string.

                           You can use this curly bracket syntax to insert more complex variable values, such as array element
                         values and object properties. (You explore arrays and objects in the next few chapters.) Just make sure
                         the whole expression is surrounded by curly brackets, and you ’ re good to go:
                             $myArray[“age”] = 34;

                             echo “My age is {$myArray[“age”]}”; // Displays “My age is 34”


                                                                                                          75





                                                                                                      9/21/09   8:53:40 AM
          c05.indd   75
          c05.indd   75                                                                               9/21/09   8:53:40 AM
   108   109   110   111   112   113   114   115   116   117   118