Displaying random "ads" from an XML document
If you can use any format, use serialized PHP, it's by far the fastest.

&lt;?php

class Ad {

  public $alt;

  public $departmentName;

  public $linkTo;

  public $name;

}

function formatAd(Ad $ad) {
  return "&lt;div class='adRight'&gt;&lt;a href='{$ad-&gt;linkTo}'&gt;&lt;img src='../../includes/images/adImages/{$ad-&gt;name}' alt='{$ad-&gt;alt}'&gt;&lt;/a&gt;&lt;/div&gt;";
}

function rightAds($departmentName, $displayNumber) {
  static $ads = null;

  if (!$ads) {
    $ads = unserialize(file_get_contents(__DIR__ . "/smallAdsByDepartment.ser"));
  }

  $pt = $departmentName === "pt";

  /* @var $ad Ad */
  foreach ($ads as $delta =&gt; $ad) {
    if (strpos($ad-&gt;departmentName, $departmentName) !== false || strpos($departmentName, "all") !== false) {
      $noPT = strpos($ad-&gt;departmentName, "noPT") !== false;
      if (($pt &amp;&amp; $noPT) || (strpos(__DIR__, "pt") !== false &amp;&amp; basename($_SERVER["REQUEST_URI"]) === "admissions.php" &amp;&amp; $noPT) || ($departmentName === "cls" &amp;&amp; strpos($ad-&gt;departmentName, "noCLS") !== false)) {
        unset($ads[$delta]);
      }
    }
  }

  if ($displayNumber &lt;= 1) {
    echo formatAd($ads[mt_rand(0, count($ads) - 1)]);
  }
  else {
    $keys = range(0, count($ads) - 1);
    shuffle($keys);
    $c = count($keys);
    for ($i = 0; $i &lt; $c; ++$i) {
      echo formatAd($ads[$keys[$i]]);
      if ($i === $displayNumber) {
        break;
      }
    }
  }
}