Jump to content

Recommended Posts

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]
<?php
if ($_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.
Link to comment
https://forums.phpfreaks.com/topic/36489-solved-php-error-notices/
Share on other sites

"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*
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.
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?
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 set
if (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]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 set
if (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 set
if (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?
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.