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.

CHAPTER 5 • ARRAYSwww.it-ebooks.infoThe is_array() function determines whether variable is an array, returning TRUE if it is and FALSEotherwise. Note that even an array consisting of a single value will still be considered an array. Anexample follows:$states = array("Florida");$state = "Ohio";printf("\$states is an array: %s ", (is_array($states) ? "TRUE" : "FALSE"));printf("\$state is an array: %s ", (is_array($state) ? "TRUE" : "FALSE"));Executing this example produces the following:$states is an array: TRUE$state is an array: FALSEOutputting an ArrayThe most common way to output an array’s contents is by iterating over each key and echoing thecorresponding value. For instance, a foreach statement does the trick nicely:$states = array("Ohio", "Florida", "Texas");foreach ($states AS $state) {echo "{$state}";}If you want to print an array of arrays or need to exercise a more exacting format standard over arrayoutput, consider using the vprint() function, which allows you to easily display array contents using thesame formatting syntax used by the printf() and sprintf() functions introduced in Chapter 3. Here’san example:$customers = array();$customers[] = array("Jason Gilmore", "jason@example.com", "614-999-9999");$customers[] = array("Jesse James", "jesse@example.<strong>net</strong>", "818-999-9999");$customers[] = array("Donald Duck", "donald@example.org", "212-999-9999");foreach ($customers AS $customer) {vprintf("Name: %sE-mail: %sPhone: %s", $customer);}Executing this code produces the following output:Name: Jason GilmoreE-mail: jason@example.comPhone: 614-999-9999Name: Jesse James108

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

Saved successfully!

Ooh no, something went wrong!