C++ preprocessor #define
is totally different.
Is the PHP define()
any different than just creating a var?
define(\"SETTING
Not sure about efficiency, but it is more than creating a var:
php > $cat='';$f=microtime(1);$s='cowcow45';$i=9000;while ($i--){$cat.='plip'.$s.'cow';}echo microtime(1)-$f."\n";
0.00689506530762
php > $cat='';$f=microtime(1);define('s','cowcow45');$i=9000;while ($i--){$cat.='plip'.s.'cow';}echo microtime(1)-$f."\n";
0.00941896438599
This is repeatable with similar results. It looks to me like constants are a bit slower to define and/or use than variables.
Main differences:
2020 update (PHP 7.2, AMD Ryzen9, Zend OpCache enabled)
summary: redefining the same constant is slow. checking and defining constants vs $_GLOBALS is about 8x slower, checking undefined constants is slightly slower. Don't use globals.
CODE:
$loops = 90000;
$m0 = microtime(true);
for ($i=0; $i<$loops; $i++) {
define("FOO$i", true);
}
$m1 = microtime(true);
echo "Define new const {$loops}s: (" . ($m1-$m0) . ")\n";
// etc...
OUTPUT:
Define new const 90000s: (0.012847185134888)
Define same const 90000s: (0.89289903640747)
Define same super global 90000s: (0.0010528564453125)
Define new super global 90000s: (0.0080759525299072)
check same undefined 90000s: (0.0021710395812988)
check same defined 90000s: (0.00087404251098633)
check different defined 90000s: (0.0076708793640137)