PHP - Undefined variable errors

Discussion in 'PHP' started by unispace, Dec 13, 2006.

  1. #1
    I just setup a new server running php5, an upgrade from the last server running php4 and I'm migrated all my websites to the new server with php5.

    I keep getting these "Undefined variable" errors for variable that haven't been set. I know I can could code in an isset() for every variable in an if statement to work around this but is there any other way that isn't so tedious. Maybe a setting in php.ini to ignore it?

    Thanks for any help.
     
    unispace, Dec 13, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Try placing this at the top of your page.

    
    
    error_reporting(E_ALL & ~E_NOTICE);
    
    PHP:
     
    nico_swd, Dec 13, 2006 IP
  3. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Or if you still want errors to debug but not on certain variables just put a '@' in front of them.
     
    CodyRo, Dec 13, 2006 IP
  4. crazyden

    crazyden Guest

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is bad. All this is bad. It seems like your PHP4 code was a little bit not correct, and was ignoring variable setting (99% php developers do so). You can use @, error_reporting, ini_set, htaccess, php.ini to turn off reporting for notices, but remember - small notice leads to huge errors in the end
     
    crazyden, Dec 13, 2006 IP
  5. unispace

    unispace Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks guys, all very helpful. It's been a while since i setup php on a server.

    I know not setting and checking varibles is BAD but that is why I like it, haha - I'm such a php loser.
     
    unispace, Dec 13, 2006 IP
  6. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #6
    Yeah. Always make sure a variable exists before using them!!

    use something like this:

    
    $var = $var ? $var : "";
    
    PHP:
    This above will set the variable if it is not set.
     
    drewbe121212, Dec 13, 2006 IP
  7. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #7
    no need to do this. all you need to do is to go to php.ini and set:

    error_reporting = E_ALL & ~E_NOTICE
    Code (markup):
     
    daboss, Dec 13, 2006 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Yep, but some people don't get access to their php.ini file on their server.
     
    nico_swd, Dec 14, 2006 IP
  9. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Could set it in .htaccess :)
     
    CodyRo, Dec 14, 2006 IP
  10. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Or use ini_set... or, god forbid, code it right? Just because you can do something, doesn't mean you should!
     
    TwistMyArm, Dec 14, 2006 IP
  11. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #11
    I have different settings for different scripts. For instance if its juts some raggy application for personal use (like maybe a cron update) I will go without any error reporting at all. Any site that goes live though will get the ol E_ERROR ^ E_WARNING

    :)
     
    drewbe121212, Dec 14, 2006 IP
  12. Krownet

    Krownet Peon

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Code it right :confused:

    But that means you need to create more blank spaces between the lines... and more words... it's a whole nother 15 BYTES!.. lolol

    :eek:
     
    Krownet, Dec 14, 2006 IP
  13. rajeev_seo

    rajeev_seo Peon

    Messages:
    211
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    first on error reporting by error_reporting(E_ALL,"");
    then check its
     
    rajeev_seo, Apr 24, 2011 IP