Ninjakreborn Posted January 31, 2007 Share Posted January 31, 2007 I had an older client of mine (I created a site for, in place of his boss. He was actually supposed to be building it). He came to me, and told me that his programmer is complaining of using error reporting to turn off Notices. Notices are something that are a natural part of programming, I get notices for just using[code]<?phpif ($_SESSION['logged'] == "yes")?>[/code]To test if someone is logged in. Are getting notices good/bad, does someone have a few links on why it is good/bad, and how a program with them all over the place can be fixed. I always program with it off, because it annoys me, where I read different areas on Google, they are not something to worry about. I have also heard they are no worse than the warnings you get in CSS, which I have started ignoring over time. So in this specific post I have two questions actually, they are:Are PHP warnings good or bad?Do you have any type of proof (Link/Tutorial) that backs up the ideas of it being good/bad and why?Thank you for all the advice. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 "how a program with them all over the place can be fixed."Turn notices on, and go through and find them :)Normally it's a simple matter of using an undeclared variable, which I always found to be a good thing in PHP. I guess catching those can help eliminate risk when register_globals is on. *shrug* Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 31, 2007 Author Share Posted January 31, 2007 Ok, I guess then it's highly subject to opinion. I like using them, but I am going to have to fix them for this purpose. Thanks for the advice. Quote Link to comment Share on other sites More sharing options...
Orio Posted January 31, 2007 Share Posted January 31, 2007 I try to avoid notices as much as I can. When I am working on something, I always set error_reporting(E_ALL), because notices can sometimes reveal some annoying bugs. When the site is running, I turn them off so everything will look fine :)For an example:I made with some one a system, which includes a login system. Every page included config.php which, among other things, defined this:$config['slat'] = "u3rh934h94";My partner has very bad english- instead of writing "salt" he wrote "slat"... Some pages (that I made) used $config['salt'] and some (those my partner made) used $config['slat']. This variable, as it's name suggests, was added to all of the passwords before hashing them. But because some pages used $config['salt'], which wasn't defined and therefor had the effect of an empty string, and some used $config['slat'], which was defined, the login system gave strange errors- through some pages you could log in and through some you couldn't. I then set error_reporting(E_ALL) and got "Notice: Undefined index 'salt' in line....". Then everything became clear.So, as my story can tell, turning error reporting on when developing something is helpful. Notices may lead to poor programming so it's recommended to avoid them.That's my opinion anyway.Orio. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 Orio has a very good point.Slat, lol. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 31, 2007 Author Share Posted January 31, 2007 Ok, I have one problem though. I always do stuff like what I showed you for logins. What tips can I keep in mind to really help me start programming without getting notices. Right now if I turned any of my site's on error reporting all, I would get a lot of error's. Last time I Tried dealing with some, I spent hours, trying to figure out what was wrong, until I just turned it off. Any advice on making a clean change from programming with notices, to programming without notices? Quote Link to comment Share on other sites More sharing options...
AndyB Posted January 31, 2007 Share Posted January 31, 2007 [quote author=businessman332211 link=topic=124876.msg518050#msg518050 date=1170257987]What tips can I keep in mind to really help me start programming without getting notices[/quote]1. Declare all variables.2. Code more carefully. Quote Link to comment Share on other sites More sharing options...
Orio Posted January 31, 2007 Share Posted January 31, 2007 isset() prevents a lot of work, when dealing with variables that you are not sure they are set.Example:[code]if ($_SESSION['logged'] == "yes") //may give a notice if $_SESSION['logged'] is not setif (isset($_SESSION['logged']) && $_SESSION['logged'] == "yes") //no notice[/code]Another thing that causes lots of notices is when you don't quote array's keys. PHP looks for a constant with that name and if it's not found it understands what you meant and throws a notice.Example:[code]echo $_SESSION[username]; //Notice: Use of undefined constant username in <...> assumed 'username'.echo $_SESSION['username']; //no notice[/code]Other notices can prevent situations like the one I've described- misspells etc'.Orio. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 31, 2007 Author Share Posted January 31, 2007 [quote]1. Declare all variables.2. Code more carefully.[/quote]Thank you[quote author=Orio link=topic=124876.msg518072#msg518072 date=1170258992]isset() prevents a lot of work, when dealing with variables that you are not sure they are set.Example:[code]if ($_SESSION['logged'] == "yes") //may give a notice if $_SESSION['logged'] is not setif (isset($_SESSION['logged']) && $_SESSION['logged'] == "yes") //no notice[/code]Another thing that causes lots of notices is when you don't quote array's keys. PHP looks for a constant with that name and if it's not found it understands what you meant and throws a notice.Example:[code]echo $_SESSION[username]; //Notice: Use of undefined constant username in <...> assumed 'username'.echo $_SESSION['username']; //no notice[/code]Other notices can prevent situations like the one I've described- misspells etc'.Orio. [/quote]Ok, there is one thing that strikes me as odd here, then I will mark it as solved.In that example above, you showed my copy first. That would throw errors, then you showed the other copy, which wouldn't throw errors. When I look at it, it's the same thing.[code]if ($_SESSION['logged'] == "yes") //may give a notice if $_SESSION['logged'] is not setif (isset($_SESSION['logged']) && $_SESSION['logged'] == "yes") //no notice[/code]In your example, it is doing the same thing the upper one is doing, except you have isset testing first. WHat is different about this that prevents an error. Is it that php picks up that you first tested for isset, then tested for the session value. In the same if, so it stops the error from coming up? Quote Link to comment Share on other sites More sharing options...
trq Posted January 31, 2007 Share Posted January 31, 2007 With isset your actually testing to see if the variable exists before trying to use it. You just try to use it wether it exists or not. Quote Link to comment Share on other sites More sharing options...
redbullmarky Posted January 31, 2007 Share Posted January 31, 2007 yep just to expand - if you have a list of conditions all seperated with (for example) &&, then if any of the conditions/checks fail, none of the rest are tested. so above, using isset first means that the second bit actually checking its value never gets tested. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted January 31, 2007 Author Share Posted January 31, 2007 Ah ok, I understand now. I also see that it's not too much harder programming with notices than without, so from now on I will try to accomodate for that. Thank you for the advice. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.