can't access global variables inside a usort function?

前端 未结 4 1237
终归单人心
终归单人心 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:55

    It is working as of php 5.2.4

    $testglobal = ' WORKING ';
    $topics = array('a','b','c');      
    function cmp($a, $b) {
        global $testglobal;
        echo 'hi' . $testglobal;
    }
    usort($topics, "cmp");
    // hi WORKING hi WORKING 
    

提交回复
热议问题