简体   繁体   English

这个功能出了什么问题?

[英]What's wrong with this function?

I'm using this function to determine whether my application should be online or offline: 我正在使用此功能来确定我的应用程序是在线还是离线:

function online() {
   if ($online == "0") {
     if($_SESSION['exp_user']['userlevel'] != "1") {
          include("error/offline.php");
          exit();
                                                   } 
                        }
                   }

However, with the data value set to 0 in the database, and $online does = '0', why is error/offline.php not included for those whoose user level is not 1? 但是,如果数据库中的数据值设置为0,并且$online ='0',那么为什么用户级别不为1的用户不包含error / offline.php?

Thanks :) 谢谢 :)

What is $online , a global variable? 什么是$online ,一个全局变量? If so you have to do global $online to access it inside a function. 如果是这样,你必须在global $online中访问函数内部。 Right now $online is a default null value, which is not equal to string "0". 现在$online是一个默认的null值,它不等于字符串“0”。

"Chaos" is right about the global variables. “混沌”对全局变量是正确的。 But if you're not sure, one way to debug something like this is to add "echo" or "die" statements in various places, to see what's happening in the code. 但是如果你不确定,调试这样的东西的一种方法是在各个地方添加“echo”或“die”语句,看看代码中发生了什么。 Put one inside the first "if" statement to see if it gets that far, then one in the second "if" statement. 在第一个“if”语句中放置一个以查看它是否到达那么远,然后在第二个“if”语句中放入一个。 Echo the values of the variables you're testing, so you can tell why the conditions aren't working. 回显您正在测试的变量的值,以便了解条件不起作用的原因。

To JW's point for debugging. 致JW的调试要点。 Instead of littering your code with echos though just make a quick class such as Logger or Debug that you can call to log messages as echos. 虽然只是制作一个快速的类,如Logger或Debug,你可以调用日志消息作为回声,而不是用回声乱丢你的代码。 Or better yet use an exisitng tool such as http://www.indelible.org/php/Log/guide.html . 或者更好地使用现有的工具,例如http://www.indelible.org/php/Log/guide.html This will let you debug in [FirePHP in Firefox][2] and never have to clean up echo statements again. 这将允许您在[FirePHP in Firefox] [2]中进行调试,而不必再次清理echo语句。 Or just use Firebug directly if you only plan on using it for debug in browser iteration testing. 或者只是直接使用Firebug,如果您只打算在浏览器迭代测试中使用它进行调试。

You can clean them all up later or use it as a code logger which should be in most larger applications for error logging and reporting metrics. 您可以稍后清理它们或将其用作代码记录器,该代码记录器应该位于大多数应用程序中,以用于错误记录和报告指标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM