简体   繁体   中英

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?

Thanks :)

What is $online , a global variable? If so you have to do global $online to access it inside a function. Right now $online is a default null value, which is not equal to string "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. Put one inside the first "if" statement to see if it gets that far, then one in the second "if" statement. 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. 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. Or better yet use an exisitng tool such as 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. Or just use Firebug directly if you only plan on using it for debug in browser iteration testing.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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