title: Undefined variable error on a set variable
body: <p>I am a newbie in PHP. This is a gallery code. Though I made the gallery to work, there is an error that shows up as <code>"PHP Notice:  Undefined variable: caption_array in /home/th/public_html/gallery.php on line 13"</code>. I checked and saw that <code>caption_array</code> is set but cannot figure out why this error keeps showing up. Code below is where <code>caption_array</code> is used... any help or direction is appreciated. </p>
  
  <pre><code>// display previous and next links if more than one photo 
  else if( $pcaption ) 
  {
      mysql_query("UPDATE gallery_photos SET photo_caption =    
      REPLACE(photo_caption,'\\\','') ");
      $pcaption = str_replace("-", " ",$pcaption);
      $pcaption = str_replace("%27", "'",$pcaption);
      $pcaption = str_replace("\\", "",$pcaption);
      $result = mysql_query( "SELECT photo_caption, photo_description, photo_filename,photo_keywords FROM gallery_photos WHERE photo_caption='".addslashes($pcaption)."'" ); 
      list($photo_caption, $photo_description, $photo_filename, $photo_keywords) = mysql_fetch_array( $result ); 
  
      $nr = mysql_num_rows( $result ); 
      mysql_free_result( $result );     
  
      $p_caption = $photo_caption;
      $p_description = $photo_description;
      $p_keywords = $photo_keywords;
  
      //fill caption_array with sorted pids in current category 
  
      $result = mysql_query( "SELECT photo_caption FROM gallery_photos WHERE category_name='".addslashes($cname)."' " ); 
  
      $ct = mysql_num_rows( $result ); 
  
      while ($row = mysql_fetch_array($result)) {
          $row[0]= trim($row[0]);
          $row[0] = str_replace(" ","-",$row[0]);
          $row[0] = str_replace("'","%27",$row[0]);
          $caption_array[] = trim($row[0]); 
      }
  
      mysql_free_result( $result );
  
      if( empty($nr ) ) 
      { 
          $result_final = "\t&lt;tr&gt;&lt;td&gt;***No Photo found*******&lt;/td&gt;&lt;/tr&gt;\n"; 
      } 
      else 
      { 
          $category_name = $cname; 
          $cname = str_replace(" ", "-", $cname); 
          $result_final = "
              &lt;div class=limagePage&gt;
                  &lt;div class=llink&gt;
                      &lt;a href=/gallery.php&gt;ALBUMS&lt;/a&gt;
                      &lt;span class=arrow&gt;&amp;gt;&amp;gt&lt;/span&gt;
                      &lt;a href=/gallery.php?cname=$cname&gt;$category_name&lt;/a&gt;
                  &lt;/div&gt;
          ";
  
          // display previous and next links if more than one photo 
  
          if ($ct &gt; 1) 
          { 
              $pcaption = trim($pcaption);
              $pcaption = str_replace(" ","-",$pcaption);
              $pcaption = str_replace("'","%27",$pcaption);
              $key = array_search($pcaption , $caption_array); 
              $prev = $key - 1; 
              if ($prev &lt; 0) $prev = $ct - 1; 
              $next = $key + 1; 
  
              if ($next == $ct) $next = 0; 
              $total_count= count($caption_array);
  
              $result_final .= "&lt;div class='prevnext'&gt;"; 
              $result_final .= "&lt;span class='prev'&gt;&lt;a href=/gallery.php?cname=$cname&amp;pcaption=".$caption_array[$next]."&gt;&lt;img src=/photos/assets/left.png  border=0 &gt;&lt;/a&gt;&lt;/span&gt;"; 
              $result_final .= "&lt;span class='next'&gt;&lt;a href=/gallery.php?cname=$cname&amp;pcaption=".$caption_array[$prev]."&gt;&lt;img src=/photos/assets/right.png  border=0 &gt;&lt;/a&gt;&lt;/span&gt;"; 
              $result_final .= "&lt;/div&gt;"; 
          }            
      }
  
     $cname = str_replace(" ", "-", $cname);
  
     $images_dir =str_replace(".","",$images_dir);
  
     $result_final .= "&lt;div class=limage&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;table class=image&gt;&lt;tr&gt;\n\t&lt;td&gt;&lt;a href=/gallery.php?cname=$cname&amp;pcaption=".$caption_array[$next]."&gt;&lt;img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_keywords."' /&gt;&lt;/a&gt;
          &lt;div class=caption&gt;".$photo_caption."&lt;/div&gt; 
          &lt;div class='excerpt'&gt;".$photo_description."&lt;/div&gt; 
          &lt;/td&gt;                    
          &lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;div class=underline&gt;&lt;/div&gt;&lt;/div&gt;
          &lt;!-- .limagePage --&gt;&lt;/div&gt;  ";
  }
  </code></pre>