Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder durangod's Avatar
    Join Date
    Nov 2010
    southwestern USA
    Thanked 47 Times in 45 Posts

    new define notices now and not before


    i am not sure what it is but i have noticed lately that i have gotten alot of these

    Notice: Undefined index: $value..............blah blah blah

    This is something new and im not sure when this requirement began, i cleared all my notices on my last version of my script.

    And now that i have gone to mysqli (which i cant see how that would make a difference here) i am starting to get these again. I am on php 5.3 right now and im not sure why every $value now it seems has to be defined ahead of time. I just never got so many before till now.

    One in particular is just an error filter routine $value, when the page returns it runs thru a error value filter and displays error text on the screen.

    UPDATE: i think i just figured it out, it think its because i was trying to save some keystrokes and i got rid of some of my isset usage.... i think thats why...
    Last edited by durangod; 02-08-2013 at 12:15 AM.

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    That would probably do it yeah.
    PHP Code:
    if (isset($_POST['avalue']))
    $avalue $_POST['avalue'];

    for example would create $avalue out of $_POST['avalue'] so long as it exists. The downside is, if you don't contain this than $avalue is not available at all and any attempts to read it would trigger an undefined variable error.

    Easiest way to save keystrokes it to use a ternary and give it a default:
    PHP Code:
    $avalue = isset($_POST['avalue']) ? $_POST['avalue'] : 'a default value.'
    Which guarantees $avalue will be set and will have a value regardless of if $_POST['avalue'] is set. Beyond this, you now operate on $avalue to perform validation and verification which may or may not fail your business rules, but will no longer throw an undefined offset nor undefined variable error.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  3. Users who have thanked Fou-Lu for this post:

    durangod (02-08-2013)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts