defined('YII_DEBUG') in index.php

This is the code for


// remove the following line when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);

bool defined ()

Returns TRUE if the named constant given by name has been defined, FALSE otherwise.

bool define ()

Defines a named constant at runtime.

My question is why use a bool operator "or" to link them together?

Why not the following way:


if (!defined('YII_DEBUG'))

define('YII_DEBUG',true);

My hypothesis is that in the "or" statement, the bool value is calculated one by one. If the first is true, the the whole statement just return truth. The second part(


define('YII_DEBUG',true)

) will never get executed.

What do you think?

I think that is absolutely the same. Maybe is like that for a question of efficency, but not sure.

Correct.

Read about "associativity" here:

http://de3.php.net/m…precedence.php

I often use this, to prevent having too many if()'s. It’s a nice shortcut that comes in handy e.g. when i do this:




YII_DEBUG && Yii::trace( some_expensive_conversion_method_here(),'application.controllers');



Even though Yii::trace() only logs in debug mode, it still gets called and the expensive conversion method would also get executed. Using the above construct is a simple one liner that stops evaluating the expression if the left hand side is false.

But, Mike, what if YII_DEBUG is not defined?

It’s always defined in Yii applications. See top of YiiBase.php.