Displaying random "ads" from an XML document I have an XML document that contains "ads" and includes information about the department name, image name, link to URL, and alt text.  I have a PHP function that accepts two arguments, department name and the number of "ads" to display.  It reads through the entire XML document, and stores the information in arrays if the department name matches what is passed to the argument or matches  "all".  What I have works, but it seems rather long and cumbersome.  Is there a way to shorten it and improve performance?

PHP

function rightAds($deptName, $displayNumber) {
  //load xml
  /* --- Original loading, left in for completeness  
  $completeurl = "http://example.com/example.xml";
  $xml = simplexml_load_file($completeurl);    
  --- */

  //Loading without an HTTP call, as per Corbin's point
  $xml = simplexml_load_file(dirname(__FILE__)."/smallAdsByDepartment.xml");   

  $smallAds = $xml-&gt;smallAds;

  //number of ads that can be used for department
  $numberForDepartment = 0;     

  //loop through xml document, save the ads that are "all" or the department name
  foreach($smallAds as $ad) {

    //get department name
    $departmentName = $ad-&gt;departmentName;

    //if it is a valid department, add info to corresponding arrays        
    if((strpos($departmentName, $deptName) !== false) || (strpos($departmentName, "all") !== false)) {

      //PT exception.  If department name includes noPT, and provided name is pt, don't include the image
      if(($deptName == "pt" &amp;&amp; strpos($departmentName, "noPT") !== false) || (strpos(dirname($_SERVER['PHP_SELF']), "pt") !== false &amp;&amp; basename($_SERVER['REQUEST_URI']) == "admissions.php" &amp;&amp;  strpos($departmentName, "noPT") !== false )) {
        continue;                
      }
      //CLS exception. 
      if(($deptName == "cls" &amp;&amp; strpos($departmentName, "noCLS") !== false)) {
        continue;
      }

      $name[$numberForDepartment] = $ad-&gt;name;
      $alt[$numberForDepartment] = $ad-&gt;alt;
      $linkTo[$numberForDepartment] = $ad-&gt;linkTo;
      $numberForDepartment++;
    }
  }

  //if $displayNumber is 1, or an invalid negative number, display random ad.
  if($displayNumber &lt;= 1) { 
    $randKeys = mt_rand(0, $numberForDepartment);
    echo "&lt;div class='adRight'&gt;&lt;a href='$linkTo[$randKeys]'&gt;&lt;img src='../../includes/images/adImages/$name[$randKeys]' alt='$alt[$randKeys]'&gt;&lt;/a&gt;&lt;/div&gt;";
  }
  else {
        //get a list of randomly selected keys from the array
        $randKeys = range(0, (count($name) - 1));
        shuffle($randKeys);

        $numberOfAds = 0;

        //loop through and display image/link for each 
        foreach($randKeys as $adNumber) {     
            echo "&lt;div class='adRight'&gt;&lt;a href='" . $linkTo[$adNumber] . "' tabindex='0'&gt;&lt;img src='../../includes/images/adImages/" . $name[$adNumber] . "' alt='" . $alt[$adNumber] . "'&gt;&lt;/a&gt;&lt;/div&gt;";
            $numberOfAds++;
            if($numberOfAds == $displayNumber) { break; }
        }
   }
}


Example XML



&lt;smallAds&gt;
    &lt;departmentName&gt;all&lt;/departmentName&gt;
    &lt;name&gt;smallAd3.jpg&lt;/name&gt;
    &lt;alt&gt;Faculty Research&lt;/alt&gt;
    &lt;linkTo&gt;http://example.com/research/index.php&lt;/linkTo&gt;
&lt;/smallAds&gt;
&lt;smallAds&gt;
    &lt;departmentName&gt;all, noPT&lt;/departmentName&gt;
    &lt;name&gt;smallAd6.jpg&lt;/name&gt;
    &lt;alt&gt;Advising&lt;/alt&gt;
    &lt;linkTo&gt;http://example.com/advising.php&lt;/linkTo&gt;
&lt;/smallAds&gt;
&lt;smallAds&gt;
    &lt;departmentName&gt;all, noCLS&lt;/departmentName&gt;
    &lt;name&gt;checkYouTube.png&lt;/name&gt;
    &lt;alt&gt;Check us out on YouTube&lt;/alt&gt;
    &lt;linkTo&gt;http://www.youtube.com/user/example&lt;/linkTo&gt;
&lt;/smallAds&gt;
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;
      }
    }
  }
}