php将文件内容读取到静态数组

I am writing a code for Bayesian Filter. For a particular word, I want to check if the word is in the stop words list or not, I populate from stop word list from a file on my pc. Because I have to do this for many words I don't want to read the StopWord file from my pc again and again.

I want to do something like this

function isStopWord( $word ){

       if(!isset($stopWordDict))
       {
           $stopWords = array();
           $handle = fopen("StopWords.txt", "r");
           if( $handle )
           {
               while( ( $buffer = fgets( $handle ) ) != false )
               {
                   $stopWords[] = trim( $buffer );
               }
           }
           echo "StopWord opened";
           static $stopWordDict = array();
           foreach( $stopWords  as $stopWord )
               $stopWordDict[$stopWord] = 1;
       }

      if( array_key_exists( $word, $stopWordDict ) )
           return true;
      else
          return false;
  }

I thought by using a static variable it will solve the issue, but it doesn't. Kindly help.

Put the static declaration at the beginning of the function:

function isStopWord( $word ){
   static $stopWordDict = array();
   if(!$stopWordDict)
   {
       $stopWords = file("StopWords.txt");
       echo "StopWord opened";
       foreach( $stopWords  as $stopWord ) {          
           $stopWordDict[trim($stopWord)] = 1;
       }
   }

  if( array_key_exists( $word, $stopWordDict ) )
       return true;
  else
      return false;
}

This will work since an empty array is considered falsy.