Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts

    new define notices now and not before

    hi,

    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-07-2013 at 11:15 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 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 ;)

  • Users who have thanked Fou-Lu for this post:

    durangod (02-07-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
    •