How efficient is define in PHP?

前端 未结 10 1366
不思量自难忘°
不思量自难忘° 2020-12-15 18:38

C++ preprocessor #define is totally different.

Is the PHP define() any different than just creating a var?

define(\"SETTING         


        
10条回答
  •  [愿得一人]
    2020-12-15 19:12

    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.

提交回复
热议问题