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

Unit 5: Strings



            5.2.1 echo                                                                            Notes
            To put a string into the HTML of a PHP-generated page, use echo. While it looks and for the
            most part behaves like a function, echo is a language construct. This means that you can omit the
            parentheses, so the following are equivalent:
            echo “Printy”; echo(“Printy”); // also valid
            You can specify multiple items to print by separating them with commas:
            echo “One”, “Two”, “three”; OneTwothree
            It is a parse error to use parentheses when trying to echo multiple values:
            // this is a parse error echo (“Hello”, “world”);
            Because echo is not a true function, you cannot use it as part of a larger expression:
            // parse error if (echo (“test”)) {echo (“it worked!”) ;}
            Such errors are easily remedied, though, by using the print ( ) or printf( ) functions.
            5.2.2 print ( )
            The print ( ) function sends one value (its argument) to the browser. It returns true if the string
            was successfully displayed and false otherwise (e.g. if the user pressed the Stop button on browser
            before this part of the page was rendered):
            if (! print(“Hello, Dear”)) { die(“you are not listening to me!”); } Hello, Dear.
            5.2.3 printf( )

            The printf( ) function outputs a string built by substituting values into a template (the format string).
            It is derived from the function of the same name in the standard C library. The first argument to
            printf( ) is the format string. The remaining arguments are the values to be substituted in.
            A %character in the format string indicates a substitution.

            Format Modifiers
            Each substitution marker in the template consists of a percent sign (%), possibly followed by
            modifiers from the following list, and ends with a type specifier. (Use ‘%%’ to get a single percent
            character in the output.) The modifiers must appear in the order in which they are listed here:

               •  A padding specifier denoting the character to use to pad the results to the appropriate string
                 size. Specify 0, a space, or any character prefixed with a single quote. Padding with spaces
                 is the default.
               •  A sign. This has a different effect on strings than on numbers. For strings, a minus (–) here
                 forces the string to be right-justified (the default is to left-justify). For numbers, a plus (+)
                 here forces positive numbers to be printed with a leading plus sign (e.g. 35 will be printed
                 as +35).
               •  The minimum number of characters that this element should contain. If the result is less
                 than this number of characters, the sign and padding specifier govern how to pad to this
                 length.
               •  For floating-point numbers, a precision specifier consisting of a period and a number;
                 this dictates how many decimal digits will be displayed. For types other than double, this
                 specifier is ignored.

            Type Specifiers
            The type specifier tells printf( ) what type of data is being substituted. This determines the
            interpretation of the previously listed modifiers. There are eight types, as listed in Table 5.2.






                                             LOVELY PROFESSIONAL UNIVERSITY                                   101
   102   103   104   105   106   107   108   109   110   111   112