Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of "defined('YII_DEBUG') or" in "defined('YII_DEBUG') or define('YII_DEBUG', false);"?

Tags:

yii

yii2

In the defined('YII_DEBUG') or define('YII_DEBUG', false); line of code we are checking if debug has defined previously it will do nothing but if not it will set to false.

I did not get this I mean what if we want to override previous value and why we cant simply do define('YII_DEBUG', false); why it necessary to check previous value if we don't want to use that?

like image 584
Stack user Avatar asked Nov 06 '15 11:11

Stack user


3 Answers

I didnt do it but you are not clear about my question, we can only change the YII_DEBUG value in /web/index.php. one more thing if YII_DEBUG has defined anywhere else but after that if we want to change its value what to do for that as 'or' will not change it and it is constant too so cant change its value?


Yes, you are very wrong... You can declare the YII_DEBUG value anywhere where you want, but... if it is redefined: Notice: Constant YII_DEBUG already defined in... I think thats the reason of defined() or... TO PREVENT THIS ERROR

like image 131
Fernando Avatar answered Nov 12 '22 13:11

Fernando


You can just change it to true or false to a page on the fly by just doing this:

define('YII_DEBUG', true);

In such cases defined('YII_DEBUG') or define('YII_DEBUG', false); comes in handy it checks if YII_DEBUG was true or false, if it finds YII_DEBUG has already been set to true or false somewhere else then it doesn't executes the or part.

This defined('YII_DEBUG') or define('YII_DEBUG', true); is equivalent to

if (!defined('YII_DEBUG')) {
    define('YII_DEBUG', true);
}

So, you see it checks if YII_DEBUG has been defined somewhere else, if not then it sets to true in this case.

Edit:

To debug any page on the fly, you can just do this:

if (isset($_GET['debug'])) define('YII_DEBUG', true);

of course you will have to change your url then, for example:

www.example.com/site/myAction to www.example.com/site/myAction/debug/true and remove it from index.php

EDIT 2:

Its not mandatory to define YII_DEBUG in index.php, it is aleady defined in Yii applications, you can find it in root yii.php file in case of Yii2 and in case of Yii1 its defined in framework/YiiBase.php

like image 34
Criesto Avatar answered Nov 12 '22 12:11

Criesto


The

<?php
  defined('YII_DEBUG') or define('YII_DEBUG', true);
  defined('YII_ENV') or define('YII_ENV', 'dev');

are defined in yourApp/web/index.php

and this costant define a level of debug info. In development enviroment normally this flag is set to true and then in in case of error there are show detailed information regarding the error and the code gerating the error, if false not or few information are show.

Tipically in production eviroment this costant is set to false. so minus information are show to the user.

for the defined or define

is the costant is already defined the not need define otherwise php define the constat. see php doc http://php.net/manual/en/function.defined.php defined — Checks whether a given named constant exists

You can find more information in Yii2 doc http://www.yiiframework.com/doc-2.0/guide-runtime-handling-errors.html

like image 1
ScaisEdge Avatar answered Nov 12 '22 12:11

ScaisEdge