Page 126 - Beginning PHP 5.3
P. 126

Part II: Learning the Language
                   To convert a string to all lowercase, use  strtolower()  . This function takes a string to convert, and
                 returns a converted copy of the string:
                    $myString = “Hello, world!”;
                    echo strtolower( $myString ); // Displays ‘hello, world!’

                   Similarly, you can use  strtoupper()  to convert a string to all uppercase:

                    $myString = “Hello, world!”;
                    echo strtoupper( $myString ); // Displays ‘HELLO, WORLD!’

                    ucfirst()  makes just the first letter of a string uppercase:
                    $myString = “hello, world!”;
                    echo ucfirst( $myString ); // Displays ‘Hello, world!’





                    lcfirst()  – –  introduced in PHP 5.3  —  makes the first letter of a string lowercase:
                    $myString = “Hello, World!”;

                    echo lcfirst( $myString ); // Displays ‘hello, World!’
                  Finally,  ucwords()  makes the first letter of each word in a string uppercase:
                    $myString = “hello, world!”;
                    echo ucwords( $myString ); // Displays ‘Hello, World!’

                   Speaking of upper -  and lowercase, most of the search and replacement functions described earlier in the
                 chapter are  case - sensitive . This means that they ’ ll only match letters of the same case. For example:

                    $myString = “Hello, world!”;
                    // Displays “Not found”
                    if ( strstr( $myString, “hello” ) )
                      echo “Found”;
                    else

                      echo “Not found”;
                   However, PHP includes  case - insensitive  versions of many string functions, which means they ’ ll work
                 even if the case of the strings don ’ t match. For example, there ’ s a case - insensitive version of   strstr() ,
                called   stristr()  :

                    $myString = “Hello, world!”;
                    // Displays “Found”
                    if ( stristr( $myString, “hello” ) )
                      echo “Found”;
                    else

                      echo “Not found”;






              88





                                                                                                      9/21/09   8:53:45 AM
          c05.indd   88
          c05.indd   88                                                                               9/21/09   8:53:45 AM
   121   122   123   124   125   126   127   128   129   130   131