Image

PHP - Printing Arrays - Printing Arrays

Printing Arrays

Problem
You want to print the contents of an array.
Solution
Use PHP’s print_r() or var_dump() functions:
<?php
// define array
$data = array(
"UK" =>array(
"longname" => "United Kingdom", "currency" => "GBP"),
"US" =>array(
"longname" => "United States of America", "currency" =>↵
"USD"), "IN" =>array(
"longname" => "India", "currency" => "INR"));
// print array contents
print_r($data);
var_dump($data);
?>

Comments

The print_r() and var_dump() functions are great ways to X-ray the contents of an array variable, and print a hierarchical listing of its internals. The previous listing

C h a p t e r 4 :Wo r k i n g w i t h A r r a y s 103

demonstrates them both in action. Note that var_dump() produces more verbose output (including information on data types and lengths) than print_r()