开发者

Variable Functions

I'm needing to create variable function names, the code I have:

$list = array('testa','testb','testc');

foreach($list as $type) {

    $type = function() { 
      echo " <p>$type</p&g开发者_Go百科t;"; 

    }; 

}

But I keep getting "Parse error: syntax error, unexpected T_FUNCTION in functions.php on line 69"

I had it working on my local server, but when I uploaded it, I get that error. Any ideas?


Your code looks like valid PHP 5.3.

But it's not valid PHP 5.2 : anonymous functions are one of the things added by PHP 5.3.

I' guessing your development environment is using PHP 5.3, and your server is working with PHP 5.2.


As a sidenote, not sure what you're exactly trying to do, but if you just want to display what's in your array, you could simply use :

foreach($list as $type) {
    echo " <p>$type</p>";
}

There's two strange thing in your code :

  • you are using the same $type variable as the current element of the foreach loop, and for the anonymous function.
  • And you are not importing the $type variable inside your anynomous function -- you should, to be able to use it, echoing it.


Update php on your production server to 5.3.x

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜