#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    Are php notices that important? It takes quite some time to declare all variables.


    Hello;

    Are php notices that important? It takes quite some time to declare all variables.

    Isn't it a better idea to just turn notices off rather than spending time on it?

    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    the problem with notices (or any error messages) is that computers cannot distinguish between you being sloppy on purpose or you making an actual mistake.

    Let's say there's an undefined index. There are two possible cases: You do not expect the entry to exist, but you've simply left out the isset() test out of laziness. Or you do expect a valid entry, but you've made a typo, or the entry doesn't exist due to a previous bug, or whatever.

    If you turn off notices, you ignore both cases. This means you may have serious errors in your code, but you tell PHP to keep quiet. This is obviously a bad idea.

    The solution is to write correct code that doesn't generate any notices. Yes, you may need a bit more boilerplate code. But on the other hand, you won't miss any bugs when they happen.

    Don't sacrifice quality and correctness just to save yourself a few keyboard strokes. PHP is already pretty sloppy and gets out of your way as much as possible. Most other languages are much stricter and make you work much harder to satisfy the compiler/interpreter. Don't abuse that sloppiness.

    Comments on this post

    • requinix agrees : that is an excellent explanation
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    When I came into my current job the first thing I did with our application was turn on notice/warning reporting in my dev environment, which nobody had done previously. I was lambasted with messages and I ended up going through pretty much the entire code base cleaning things up.

    Sometimes it was simply adding an isset() check, sometimes it was declaring a variable better, and sometimes actual bugs were revealed.

    Needless to say it's a lot better to have programming habits that prevent these from occurring to begin with, rather than finding yourself someday going back and having to correct them all.

    And if you're contracting for someone and give them code that they turn around and run with notice/warnings on, them seeing a slew of messages will reflect poorly on you.
    LinkedIn: Dave Mittner
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Have to agree, I'd prefer to be 'forced' into variable declaration, but I do it out of habit anyway.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    It takes quite some time to declare all variables.
    PHP doesn't have declarations the way other languages do. What, exactly, is the line of code which takes too long?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo