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

Unit 5: Strings



            Unlike in some shell environments in PHP, strings are not repeatedly processed for interpolation.   Notes
            Instead, any interpolations in a double-quoted string are processed, then the result is used as the
            value of the string:

            $bar = ‘this is not printed’; $foo = ‘$bar’; // single quotes print(“$foo”); $bar
            5.1.2 Single-Quoted Strings

            Single-quoted strings do not interpolate variables. Thus, the variable name in the following string
            is not expanded because the string literal in which it occurs is single-quoted:

            $name = ‘Pradip’; $str = ‘Hi, $navneet’; // single-quoted echo $str; Hi, $Navneet
            The only escape sequences that work in single-quoted strings are \’, which puts a single quote
            in a single-quoted string, and \\, which puts a backslash in a single-quoted string. Any other
            occurrence of a backslash is interpreted simply as a backslash:
            $name =  ‘Tim O\’Reilly’; // escaped single quote echo $name; $path = ‘C:\\WINDOWS’; //
                   escaped backslash echo $path; $nope = ‘\n’; // not an escape echo $nope; Tim O’Reilly
                   C:\WINDOWS \n





            <?php
            echo ‘this is a simple string’;
            echo ‘You can also have embedded newlines in

            strings this way as it is
            okay to do’;
            // Outputs: Arnold once said: “I will be back”
            echo ‘Arnold once said: “I\ will be back”’;

            // Outputs: You deleted C:\*.*?
            echo ‘You deleted C:\\*.*?’;

            // Outputs: You deleted C:\*.*?
            echo ‘You deleted C:\*.*?’;
            // Outputs: This will not expand: \n a newline
            echo ‘This will not expand: \n a newline’;

            // Outputs: Variables do not $expand $either
            echo ‘Variables do not $expand $either’;
            ?>

            5.1.3 Double-Quoted Strings
            Double-quoted strings interpolate variables and expand the many PHP escape sequences. Table
            5.1 lists the escape sequences recognized by PHP in double-quoted strings.








                                             LOVELY PROFESSIONAL UNIVERSITY                                    99
   100   101   102   103   104   105   106   107   108   109   110