Page 82 - Open Soource Technologies 304.indd
P. 82

Unit 6: Building Blocks of PHP



            “The result is $result\n”                                                             Notes
            “The array offset $i contains $arr[$i]”

            In cases, where you’d like to concatenate strings with values (such as variables and expressions)
            and this syntax isn’t sufficient, you can use the . (dot) operator to concatenate two or more strings.
            Single Quotes In addition to double quotes, single quotes may also delimit strings. However,
            in contrast to double quotes, single quotes do not support all the double quotes’ escaping and
            variable substitution.

            The following table includes the only two escapings supported by single quotes:
                   \’              Single quote.
                   \\              Backslash, used when wanting to represent a backslash
                                   followed by a single quote—for example, \\’.

                  Example: ‘Hello, World’

                         ‘Today\’s the day’
            6.1.2.4 Here-Docs Here-docs

            Here-Docs Here-docs enable you to embed large pieces of text in your scripts, which may include
            lots of double quotes and single quotes, without having to constantly escape them.

            The following is an example of a here-doc.

                  Example: <<<THE_END
            PHP stands for “PHP: Hypertext Preprocessor”.

            The acronym “PHP” is therefore, usually referred to as a recursive acronym ‘→because the long
            form contains the acronym itself.
            As this text is being written in a here-doc there is no need to escape the ‘→double quotes.

            THE_END

            The strings starts with <<<, followed by a string that you know doesn’t appear in your text. It is
            terminated by writing that string at the beginning of a line, followed by an optional semicolon(;),
            and then a required newline (\n). Escaping and variable substitution in here-docs is identical to
            double-quoted strings except that you are not required to escape double quotes.
            Accessing String Offsets Individual characters in a string can be accessed using the $str{offset}
            notation. You can use it to both read and write string offsets. When reading characters, this
            notation should be used only to access valid indices. When modifying characters, you may access
            offsets that don’t yet exist. PHP automatically sets that offset to the said character, and if this
            results in a gap between the ending of the original string and the offset of the new character,
            the gap filled with space characters (‘ ‘).

            This example creates and prints the string “Andi” (in an awkward way):
            $str = “A”;

            $str{2} = “d”;



                                             LOVELY PROFESSIONAL UNIVERSITY                                    77
   77   78   79   80   81   82   83   84   85   86   87