2017-05-31 40 views
-1

功能我有這樣的代碼:如何運行具有內部循環在PHP

$stopwords = array("acara", "ada", "adalah"); 
foreach ($stopwords as &$word) { 
    $word = '/\b' . preg_quote($word, '/') . '\b/'; 
} 

$filter=preg_replace($stopwords, '', $case); 

$kata = str_word_count($filter,1); 
$jumlah = count($kata); 

function myfunction() { 
    for($i=0; $i<$jumlah; $i++){ 
     echo "$kata[$i]"; 
    } 
} 

myfunction(); 

當我運行我的代碼,myfunction不顯示輸出數據。如何使用該功能獲得輸出?

+0

這肯定是最模糊的代碼,我今天見過。你的預期產出是多少?你爲什麼通過引用傳遞'$ word'?什麼是'$ case'? '$ stopwords'是一種模式?當所有變量超出範圍時,你如何期望myfunction()有任何輸出? –

+0

由於變量範圍的限制,您的$ jumlah函數內部函數不同於$ jumlah。請閱讀http://php.net/manual/en/language.variables.scope.php –

回答

2

你應該通過$卡塔和$ jumlah作爲參數:

function myfunction($jumlah, $kata) { 
     for($i=0; $i<$jumlah; $i++) 
     { 
      echo $kata[$i]; 
     } 
} 


myfunction($jumlah, $kata);