can't access global variables inside a usort function?

前端 未结 4 1234
终归单人心
终归单人心 2021-01-15 16:32

I\'m trying to do a usort in PHP, but I can\'t access global variables inside a usort function.

I\'ve simplified my code down to bare bones to show what I mean:

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 16:59

    Does it work if you access the variable using the super-global $GLOBALS array?

    $testglobal = 1;
    function cmp($a, $b) {
        echo 'hi' . $GLOBALS['testglobal'];
    }
    usort($topics, "cmp");
    

提交回复
热议问题