-1

I have this code :

$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();

When I run my code, myfunction doesn't display the output data. How to get output with that function?

yPhil
  • 8,049
  • 4
  • 57
  • 83
Atina
  • 17
  • 6
  • This is certainly the most obfuscated code I've seen today. What is your expected output? Why are you passing `$word` by reference? What is `$case`? How is `$stopwords` a pattern? How do you expect myfunction() to have any output when all the variables are out of scope? – Tim Morton May 31 '17 at 02:26
  • Your $jumlah inside function is different wth $jumlah outside the function, due to variabl scope. Please read http://php.net/manual/en/language.variables.scope.php – Muhammad Alvin May 31 '17 at 03:15

1 Answers1

2

You should pass $kata and $jumlah as parameters:

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


myfunction($jumlah, $kata);
T. AKROUT
  • 1,719
  • 8
  • 18