Variable gets lost (maybe because of ajax?)
<p>I have a fairly simple script but something is going wrong and I can't seem to figure it out. My script collects files from a folder, selects a random file. The random selected file is checked against another array to check if it's not in that array. That checked array is in fact a textstream put into an array by exploding it on the #-char.</p>

<p>The script is used to play a random sound from a collection, if the sound is not yet in a textfile it can be played. If the sound is allready in the textfile another random file must be selected (and so on.. recursion). The strange thing is that in the recursive function it all goes well but when I display the outcome from that function sometimes it just shows blank while the function for sure is returning a file to be played (and stored into the textfile).</p>

<p>The PHP script is called via the XMLHTTPObject. Could this be a reason why the variable gets lost? Or am I missing something else in my script?</p>

<h3>The script:</h3>

<pre><code>&lt;?php
    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); 

    $soundList      = get_folder_entries("playlist", array("wma", "wav", "mp3"));
    $playedFile     = "played.txt";
    $playedContents = file_get_contents($playedFile);
    $playedSounds   = explode("#", $playedContents);

    // if all files are played just select a new file and reset played.txt  
    if(count($playedSounds) == count($soundList))
    {
        $fileHandle = fopen($playedFile, "w");  
        $file       = $soundList[mt_rand(0, count($soundList) - 1)];

        fwrite($fileHandle, $file."#");
        fclose($fileHandle);

        echo "&lt;input type=\"hidden\" id=\"SoundToPlay\" value=\"".$file."\"/&gt;";
    }
    // pick a random file which has not yet been played
    else
    {
        $file       = pick_random_file($soundList, $playedSounds);
        $fileHandle = fopen($playedFile, "w");

        fwrite($fileHandle, $playedContents.$file."#");
        fclose($fileHandle);

        echo 'INPUT = '.$file;

        echo "&lt;input type=\"hidden\" id=\"SoundToPlay\" value=\"".$file."\"/&gt;";
    }

    // some debug/output info
    echo "&lt;table width=\"100%\"&gt;";
    echo "&lt;tr&gt;";
    echo "&lt;td class=\"header\"&gt;&lt;h2&gt;[Array] Playlist&lt;/h2&gt;&lt;/td&gt;";
    echo "&lt;td class=\"header\"&gt;&lt;h2&gt;[Array] Played&lt;/h2&gt;&lt;/td&gt;";
    echo "&lt;/tr&gt;";
    echo "&lt;tr&gt;";
    echo "&lt;td&gt;&lt;pre&gt;";
    print_r($soundList);
    echo "&lt;/pre&gt;&lt;/td&gt;";
    echo "&lt;td&gt;&lt;pre&gt;";
    print_r($playedSounds);
    echo "&lt;/td&gt;";
    echo "&lt;/tr&gt;";
    echo "&lt;/table&gt;";    

    // collect the files from a folder with a specific extension
    function get_folder_entries($dir, array $extensions)
    {
        $fileList = array();

        if($fileHandle  = opendir($dir))
        {
            while(false !== ($file = readdir($fileHandle)))
            {
                if($file != "." &amp;&amp; $file != ".." &amp;&amp; in_array(end(explode(".", $file)), $extensions))
                {
                    array_push($fileList, $file);
                }
            }
        }

        return $fileList;
    }

    // recursive function to pick a random sound
    function pick_random_file(array $soundList, array $playedSounds)
    {
        $sound = $soundList[mt_rand(0, count($soundList) - 1)];

        if(!in_array($sound, $playedSounds))
        {
            return $sound;  
        }
        else
        {
            pick_random_file($soundList, $playedSounds);    
        }
    }
</code></pre>
