Sometimes when debugging your code it’s useful to inspect the entire contents of an array. However, using
print()
or echo()
on an array isn’t much help:<?php $directors = array( "Kawser", "Zaman", "Sohel", "Linkon" ); // Displays "Array" echo $directors; <pre>?>
To display an entire array, use theprint_r()
function instead. Here’s an example that creates an indexed array and an associative array, then displays the contents of both arrays usingprint_r()
:
<?php $directors = array( "Sania", "Nigat", "Parvin", "GM" ); $movie = array( "title" => "Rear Window", "director" => "Alfred Hitchcock", "year" => 1954 ); echo '<h2>$directors:</h2><pre>'; print_r( $directors ); echo '</pre><h2>$movie:</h2><pre>'; print_r( $movie ); echo "</pre>"; ?>
The above code produces the following output:$directors:Array ( [0] => Alfred Hitchcock [1] => Stanley Kubrick [2] => Martin Scorsese [3] => Fritz Lang )$movie:Array ( [title] => Rear Window [director] => Alfred Hitchcock [year] => 1954 )
0 comments:
Post a Comment