11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 5 • ARRAYSE-mail: jesse@example.<strong>net</strong>Phone: 818-999-9999Name: Donald DuckE-mail: donald@example.orgPhone: 212-999-9999If you’d like to send the formatted results to a string, check out the vsprintf() function.Printing Arrays for Testing PurposesThe array contents in most of the previous examples have been displayed using comments. While thisworks great for instructional purposes, in the real world you’ll need to know how to easily output theircontents to the screen for testing purposes. This is most commonly done with the print_r() function. Itsprototype follows:boolean print_r(mixed variable [, boolean return])The print_r() function accepts a variable and sends its contents to standard output, returning TRUEon success and FALSE otherwise. This in itself isn’t particularly exciting, until you realize it will organizean array’s contents (as well as an object’s) into a readable format. For example, suppose you want toview the contents of an associative array consisting of states and their corresponding state capitals. Youcould call print_r() like this:print_r($states);This returns the following:Array ( [Ohio] => Columbus [Iowa] => Des Moines [Arizona] => Phoenix )The optional parameter return modifies the function’s behavior, causing it to return the output tothe caller, rather than send it to standard output. Therefore, if you want to return the contents of thepreceding $states array, you just set return to TRUE:$stateCapitals = print_r($states, TRUE);This function is used repeatedly throughout this chapter as a simple means for displaying exampleresults.Keep in mind the print_r() function isn’t the only way to output an array; it just offers a convenientmeans for doing so. You’re free to output arrays using a looping conditional, such as while or for; in fact,using these sorts of loops is required to implement many application features. I’ll return to this methodrepeatedly throughout this and later chapters.109

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!