The advantage / disadvantage between global variables and function parameters in PHP?

后端 未结 6 2033
生来不讨喜
生来不讨喜 2020-11-27 07:00

sorry i\'m a beginner and i can\'t determine how good a question this is, maybe it sounds utterly obvious to some of you.

if our use of these two below is t

6条回答
  •  渐次进展
    2020-11-27 07:30

    As of PHP 4 using global with big variables affects performance significantly.

    Having in $data a 3Mb string with binary map data and running 10k tests if the bit is 0 or 1 for different global usage gives the following time results:

    function getBit($pos) {
    global $data;
    $posByte = floor($pos/8); 
    ...
    }
    

    t5 bit open: 0.05495s, seek: 5.04544s, all: 5.10039s

    function getBit($data) {
     global $_bin_point;
     $pos = $_bin_point; 
     $posByte = floor($pos/8); 
    }
    

    t5 bit open: 0.03947s, seek: 0.12345s, all: 0.16292s

    function getBit($data, $pos) {
    $posByte = floor($pos/8); 
    ...
    }
    

    t5 bit open: 0.05179s, seek: 0.08856s, all: 0.14035s

    So, passing parameters is way faster than using global on variables >= 3Mb. Haven't tested with passing a $&data reference and haven't tested with PHP5.

提交回复
热议问题