SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Notice: Optimization

    I get notices in my code for accessing indexes in arrays where the index hasn't been set. My question is concerning optimization. Is it better to check my arrays and make sure the index is set before using it, or to just say, "well, they are just notices, I shouldn't worry about them." On one hand, it appears that I will be doing more calculations to check if they are set, than to just let PHP worry about it in the backend when it does happen. On the other hand, there is a reason PHP complains about it. Your thoughts?

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I might be wrong, but I think it takes more to issue a notice than to do a check on the array element. Anyways, it is simply a matter of good practice -- your program should never spit curses.
    Saul

  3. #3
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would be surprised if issuing the notice adds anything at all beyond the obvious. The parser has to do the check anyways, and once it has issuing the notice is just an extra step. On the other hand, the work involved in checking for the existence of an array key would, I imagine, depend on the size of the array.

    That said, php_daemon is 100% right. Your code should not be outputting anything except what you tell it to.
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I turn these notices off in php.ini then it won't be spitting out the "curses" but then again, it has to do some sort of checking on the backend somewhere to not overwrite data in memory. I would like a definitive answer to what is better. The other problem with doing checks, is it makes my code harder to read.

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Turning them off doesn't make your code good. The checks are not making the code less readible. The definite answer is, there must be no notices, warnings, or what not.

    That's the problem of PHP, it allows things like that. Try that in Java for example, think there's no reason for that?
    Saul

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with you php_daemon, and in any other language we couldn't get away with this, but if it was such a problem, why would it be allowed in PHP? This is one problem with PHP is it isn't strict, but that's why people like it so much. But to dynamically add indexes to associative arrays without getting these notices is a pain.

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jgstevenson View Post
    But to dynamically add indexes to associative arrays without getting these notices is a pain.
    How so?
    Saul

  8. #8
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha, I guess I wasn't thinking clearly. I was putting together a test scenario for you and this is what I figured out. Shoot, I feel like an idiot.

    I was going through some code that is doing a += on an array element that sometimes isn't assigned yet. Which, if it isn't assigned of course it should complain about doing arithmetic on something that it doesn't know what it is.

    PHP Code:
    //Problem code
    $array = array('first' => 1'second' => 1);
    $array['third']++;  //Gives notice because 'third' element isn't set.

    //Fix
    $array = array('first' => 1'second' => 1);
    isset(
    $array['third'])?$array['third']++:$array['third'] = 1
    Well, thanks for letting me talk about this and get the real problem out in the open.


Bookmarks

Posting Permissions

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