[wp-hackers] WPMU VHOST constant

Aaron D. Campbell aaron at xavisys.com
Wed Aug 5 20:25:38 UTC 2009


I'm not sure on the efficiency of warning/error suppression, and I agree 
that the difference in performance is inconsequential.  I was just 
trying to point out that you don't need constant() AND defined() except 
in the one edge case.  Either constant() with error suppression or 
defined() and use the constant directly.

One other edge case has just hit me though.  If your constant is also a 
reserved keyword such as NULL (define('NULL', 'test')) you can only 
retrieve the value using constant().  Directly using the constant name 
won't work ( if ( NULL == 'test') {/* Never executed*/}).

Anyway, it's really just optimization for the sake of optimization since 
it's such a small difference.

Will Anderson wrote:
> On Wed, Aug 5, 2009 at 3:40 PM, Aaron D. Campbell <aaron at xavisys.com> wrote:
>
>   
>> If you use constant(), you don't need really need to use defined() since it
>> returns null if the constant if undefined.  Instead you can just suppress
>> the warning, so either of these should work:
>> if ( @constant('VHOST') == 'yes' ) {}
>> if ( defined('VHOST') && VHOST == 'yes' ) {}
>>
>> The only edge case where those would perform differently is if you were
>> trying to tell if the constant was not defined vs being defined as null:
>> //MYCONST purposely not defined
>> if ( @constant('MYCONST') == null ) {
>>  //This runs
>> }
>> if ( defined('MYCONST') && MYCONST== null ) {
>>  //This does not run
>>
>> }
>>
>>     
>
> Isn't error suppression less efficient though? Once again, I'm not sure it
> makes a huge difference in code that will only run once per request, but
> it's still probably worth considering.


More information about the wp-hackers mailing list