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

扶醉桌前 提交于 2021-01-27 05:04:39

问题


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?


回答1:


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




回答2:


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




回答3:


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




回答4:


The error handler adjusts the error display according to the value of the constant YII_DEBUG. When YII_DEBUG is true (meaning in debug mode), the error handler will display exceptions with detailed call stack information and source code lines to help easier debugging. And when YII_DEBUG is false, only the error message will be displayed to prevent revealing sensitive information about the application.

Have a look at YII_DEBUG and YiiBase.php and yii.php




回答5:


defined('YII_DEBUG') or define('YII_DEBUG', false) checks if the constant YII_DEBUG was defined (regardless of its value), and if it wasn't defined earlier it defines the constant as false.

The line ensures the constant is defined so it can be used and its first part (defined('YII_DEBUG') or) ensures this line won't override it to false if it was set to true earlier.



来源:https://stackoverflow.com/questions/33566178/what-is-the-use-of-definedyii-debug-or-in-definedyii-debug-or-define

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!