Tl/dr: define const=const > parameter > global > $GLOBAL
However, the difference is really marginal, for 10 millions of operations, the difference is less than a second.
I ran the example of @Kevin Vaughan (I ran it 10'000'000 instead of 1'000'000) Windows 10 64bits, php 7.2 64bits
Pass value by parameter
Time: 0.62202191352844s
Memory: 0
Global var reference
Time: 0.70083403587341s
Memory: 0
GLOBALS array reference
Time: 0.84828305244446s
Memory: 0
GLOBALS array reference2
Time: 0.80545091629028s
Memory: 0
GLOBALS array const define
Time: 0.57029700279236s
Memory: 0
GLOBALS array const
Time: 0.57260584831238s
Memory: 0
The memory leak is gone, however, the difference of performance is really small. Yet, passing the values by parameter is still fast.
Updated code with new functions.
<?php
echo "<pre>";
$baseVar = str_repeat('x', 1000000);
$GLOBALS['myVar'] = $baseVar;
define('BASEVAR',$baseVar);
const BASEVAR2=BASEVAR;
function testfunc_param($paramVar) {
$localVar = $paramVar;
return $localVar;
}
function testfunc_global() {
global $myVar;
$localVar = $myVar;
return $localVar;
}
function testfunc_globalsarray() {
$localVar = $GLOBALS['myVar'];
return $localVar;
}
function testfunc_globalsarray2() {
return $GLOBALS['myVar'];
}
function testfunc_const() {
return BASEVAR;
}
function testfunc_const2() {
return BASEVAR2;
}
// Testing passing value by parameter
memory_get_usage(); // in case this procs garbage collection
$memoryStart = memory_get_usage(true);
$timeStart = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
testfunc_param($baseVar);
}
$timeEnd = microtime(true);
$memoryEnd = memory_get_usage(true);
print "Pass value by parameter\nTime: ".($timeEnd - $timeStart)."s\nMemory: ".($memoryEnd-$memoryStart)."\n\n";
// Testing reference to global variable
memory_get_usage(); // in case this procs garbage collection
$memoryStart = memory_get_usage(true);
$timeStart = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
testfunc_global();
}
$timeEnd = microtime(true);
$memoryEnd = memory_get_usage(true);
print "Global var reference\nTime: ".($timeEnd - $timeStart)."s\nMemory: ".($memoryEnd-$memoryStart)."\n\n";
// Testing reference to global variable via $GLOBALS
memory_get_usage(); // in case this procs garbage collection
$memoryStart = memory_get_usage(true);
$timeStart = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
testfunc_globalsarray();
}
$timeEnd = microtime(true);
$memoryEnd = memory_get_usage(true);
print "GLOBALS array reference\nTime: ".($timeEnd - $timeStart)."s\nMemory: ".($memoryEnd-$memoryStart)."\n\n";
// Testing reference to global variable via $GLOBALS
memory_get_usage(); // in case this procs garbage collection
$memoryStart = memory_get_usage(true);
$timeStart = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
testfunc_globalsarray2();
}
$timeEnd = microtime(true);
$memoryEnd = memory_get_usage(true);
print "GLOBALS array reference2\nTime: ".($timeEnd - $timeStart)."s\nMemory: ".($memoryEnd-$memoryStart)."\n\n";
// Testing reference to global variable via $GLOBALS
memory_get_usage(); // in case this procs garbage collection
$memoryStart = memory_get_usage(true);
$timeStart = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
testfunc_const();
}
$timeEnd = microtime(true);
$memoryEnd = memory_get_usage(true);
print "GLOBALS array const define\nTime: ".($timeEnd - $timeStart)."s\nMemory: ".($memoryEnd-$memoryStart)."\n\n";
// Testing reference to global variable via $GLOBALS
memory_get_usage(); // in case this procs garbage collection
$memoryStart = memory_get_usage(true);
$timeStart = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
testfunc_const2();
}
$timeEnd = microtime(true);
$memoryEnd = memory_get_usage(true);
print "GLOBALS array const\nTime: ".($timeEnd - $timeStart)."s\nMemory: ".($memoryEnd-$memoryStart)."\n\n";
echo "</pre>";