Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unexpected T_USE, expecting '{'

Tags:

syntax

php

<?php

$wordFrequencyArray = array();

function countWords($file) use($wordFrequencyArray) {  //error here
    /* get content of $filename in $content */
    $content = strtolower(file_get_contents($filename));

Here is a snippet of a code i am using.

I am getting error on the 3rd line. I have all the matching braces .What might be wrong?

like image 609
shiven Avatar asked Aug 09 '12 06:08

shiven


2 Answers

It should be:

$countWords = function($file) use($wordFrequencyArray) {
  //...
};
like image 168
xdazz Avatar answered Oct 08 '22 02:10

xdazz


Only anonymous functions may have declare a use statement, hence the error message warning you that an opening bracket is expected instead of the use statement.

To circumnavigate having no use statement, you can either add more parameters and pass it to the function, or in some cases call the variables as global.

like image 36
FThompson Avatar answered Oct 08 '22 01:10

FThompson