Chapter3.Handling Strings and Arrays


Chapter 3. Handling Strings and Arrays

Two data types merit some special attentionstrings and arrays. We've already seen strings at work, including single- and double-quoted strings (recall also that double-quoted strings allow variable interpolation). PHP also comes packed with more string power, and we're going to dig into that in this chaptertons of functions are built into PHP that work with strings, from sorting strings to searching them, trimming extra spaces off of them, and getting their lengths. We'll get a handle on those functions in this chapter.

Besides strings, we're also going to get a handle on arrays in this chapter. We've seen how to store data in simple variables, but there's more to the story here. Arrays can hold multiple data items, assigning each one a numeric or text index (also called a key). For example, if you want to store some student test scores, you can store them in an array, and then you can access each score in the array via a numeric index. That's great as far as computers are concerned because you can work through all the elements in an array simply by steadily incrementing that index, as you might do with a loop. In that way, you can use your computer to iterate over all the elements in an array in order to print them out or find their average value, for example.

Arrays represent the first time we're associating data items together. Up to this point, we've only worked with simple variables, but working with arrays is fundamental to PHP for such tasks as reading the data that users enter in web pages. We'll get the details on strings and arrays in this chapter, and I'll start with the string functions.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net