tags:

views:

2096

answers:

7

C++ preprocessor #define is totally different.

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

define("SETTING", 0);  
$something = SETTING;

vs

$setting = 0;  
$something = $setting;
+1  A: 

Here are the differences, from the manual

  • Constants do not have a dollar sign ($) before them;
  • Constants may only be defined using the define() function, not by simple assignment;
  • Constants may be defined and accessed anywhere without regard to variable scoping rules;
  • Constants may not be redefined or undefined once they have been set; and
  • Constants may only evaluate to scalar values.

For me, the main benefit is the global scope. I certainly don't worry about their efficiency - use them whenever you need a global scalar value which should not be alterable.

Paul Dixon
the syntax and scope is obvious..what I mean is performance wise
Prody
+1  A: 

Main differences:

  • define is constant, variable is variable
  • they different scope/visibility
Milan Babuškov
A: 

Not sure about efficiency, but it is more than creating a var:

  • It is a constant: you can't redefine or reassign this SETTING.
  • If the define isn't found, $something is set to "SETTING", which is useful, for example, in i18n: if a translation is missing (ie. the corresponding define is the localization file), we see a big word in uppercase, quite visible...
PhiLho
+4  A: 

In general, the idea of a constant is to be constant, (Sounds funny, right? ;)) inside your program. Which means that the compiler (interpreter) will replace "FOOBAR" with FOOBAR's value throughout your entire script.

So much for the theory and the advantages - if you compile. Now PHP is pretty dynamic and in most cases you will not notice a different because the PHP script is compiled with each run. Afai-can-tell you should not see a notable difference in speed between constants and variables unless you use a byte-code cache such as APC, Zend Optimizer or eAccelerator. Then it can make sense.

All other advantages/disadvantages of constants have been already noted here and can be found in the PHP manual.

Till
A: 

When I run speed tests, constants being set and dumped out run much a little faster than setting variables and dumping them out.

A: 
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.

Alex JL
+3  A: 

'define' operation itself is rather slow - confirmed by xdebug profiler.

Here is benchmarks from http://t3.dotgnu.info/blog/php/my-first-php-extension.html:

  • pure 'define'
    380.785 fetches/sec
    14.2647 mean msecs/first-response

  • constants defined with 'hidef' extension
    930.783 fetches/sec
    6.30279 mean msecs/first-response