Unexpected T_USE waiting for '{'

<?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 piece of code that I am using.

I get an error on the third line. I have all the matching braces. What could be wrong?

+5
source share
5 answers

It should be:

$countWords = function($file) use($wordFrequencyArray) {
  //...
};
+7
source

Only anonymous functions can be declared by an operator use, so an error message warns you that an useopening bracket is expected instead of an operator .

To circumnavigate the world without instructions use, you can either add additional parameters, or pass it to a function, or in some cases call variables like global .

+8
source

- :

$wordFrequencyArray = array();

function countWords($file,$wordFrequencyArray)  {
    /* get content of $filename in $content */
    $content = strtolower(file_get_contents($filename));
}
0

, .

I struggled to use namespace declaration and usage commands. In the end, I found that the problem was that I was trying to make an declaration in the function where I was going to use the element. This caused my script to crash with the above error message. Moving an Ad to the Top of a PHP Script After

0
source

to try

<?php

$wordFrequencyArray = array();

function countWords($file) {  
global $wordFrequencyArray;
 /* get content of $filename in $content */
 $content = strtolower(file_get_contents($filename));
}
-1
source

All Articles