Core PHP

Printing Arrays

Frequently, we will want to check the entries of an array to debug our program. By printing the entire array, we can verify that it contains what it should.

PrintArray.php

<?php
  // Create an associative array
  $saTrinity = array("Father"=>"God", "Son"=>"God", "Holy Spirit"=>"God");


  echo "<h1>Array Printing Methods:</h1>";


  echo "<h3>print_r():</h3><pre>";
  print_r($saTrinity);
  echo "</pre>";


  echo "<h3>var_dump():</h3><pre>";
  var_dump($saTrinity);
  echo "</pre>";


  echo "<h3>var_export():</h3><pre>";
  var_export($saTrinity);
  echo "</pre>";


  // Use a foreach loop to print each key with its value
  echo "<h3>Foreach loop with Key=>Value Pairs:</h3>";
  foreach ($saTrinity as $sPerson => $sEssence) {
    echo "{$sPerson} => {$sEssence}<br />";
  }
  echo "<br />";
?>
 

Output

 
 

© 2007–2025 XoaX.net LLC. All rights reserved.