1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

isset and foreach

Discussion in 'PHP' started by Omzy, Jan 22, 2009.

  1. #1
    I've got several foreach loops in one of my scripts. For example:

    foreach($_POST['tags'] as $index1 => $value1)

    Now whenever the $_POST value does not exist I get the warning message on the page. Now I know I can get around this by putting the foreach loop inside an IF statement with an isset command.

    But I was wondering if there is a better way to do this - can the $_POST value be initialized in the foreach statement? Or can the isset command be integrated in to the foreach statment?
     
    Omzy, Jan 22, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    <?php
    
    error_reporting( E_ALL );
    
    foreach ( (isset($_POST['var']) && is_array($_POST['var']) && count($_POST['var']) > 0) ? $_POST['var'] : array() as $key => $value )
    {
    	/* Do something. */
    }
    PHP:
     
    Danltn, Jan 22, 2009 IP
  3. CTThompson

    CTThompson Member

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    You can use the IF statement without the isset command. This might speed up processing a little:

    if ($_POST['var']) {

    stuff...

    }

    It'll just validate if the variable exists. Usage in this fashion will depend upon how your php.ini is configured.
     
    CTThompson, Jan 22, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Not recommended, it will trigger a warning if $_POST['var'] is not set, and it will not run if it equates to '', 0 or similar.

    Dan.
     
    Danltn, Jan 22, 2009 IP
  5. CTThompson

    CTThompson Member

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    You're right. Like I said, it depends on how your php.ini is configured. Whether or not a warning is logged will depend on your log level.

    You are also right about sending a 0 as the variable.

    However, I do feel that in certain contexts this method is acceptable and quicker.
     
    CTThompson, Jan 22, 2009 IP
  6. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    I would just add the if statement before the foreach loop. The code is much cleaner and easier to read that way.
     
    phper, Jan 22, 2009 IP
  7. SEOnetwork

    SEOnetwork Banned

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    In the last bit of code on this php mail() tutorial, it uses isset within an if/else statement, similar to what phper suggested: hxxp://www.jwrmedia.com/lessons/php/mail
     
    SEOnetwork, Jan 22, 2009 IP
  8. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Same here. That's really what if statements are for. There's no need to be clever for the sake of being clever.
     
    LogicFlux, Jan 22, 2009 IP
  9. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #9
    You can typecast it to an array inline and reduce it to an E_NOTICE for the undefined index which should be ignored by all but the strictest setups.

    foreach((array)$_POST['var'] as $key => $val)
    Code (markup):
     
    joebert, Jan 23, 2009 IP
  10. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #10
    I assuming that your form action url is same as the script itself. You can stop the script if there is no post data.
    if (empty($_POST)) exit;
    PHP:
     
    Kaizoku, Jan 23, 2009 IP
  11. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Wow, that's brilliant, just what I needed. It's a lot better than having to put each of the foreach loops in to an if(isset) statement!
     
    Omzy, Jan 23, 2009 IP
  12. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #12
    E_NOTICES means you're doing it wrong.

    Good code show have no errors whatsoever, even with error_reporting set to E_ALL;

    It's really is worth writing code so it works properly first time, helps portability for starters...

    Dan.
     
    Danltn, Jan 24, 2009 IP
  13. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #13
    Never turn E_NOTICES off. As mentioned above it will make your code unstable as well as less secure. Bugs can turn into major exploits and your site will get hacked in no time unfortunately.

    Regards,
    Dennis M.
     
    Dennis M., Jan 24, 2009 IP
  14. AimHigher

    AimHigher Member

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #14
    
    
    if (isset($_POST['tags']) && is_array($_POST['tags'])) {
    
      foreach($_POST['tags'] as $index1 => $value1) {
    
        // do something
    
      }
    
    }
    
    
    Code (markup):
     
    AimHigher, Jan 24, 2009 IP
  15. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thanks for the input guys. I didn't know about E_ALL and E_NOTICE until now. I thought my code was perfect until I switched these on, now I'm getting all sorts of undefined index errors! For example:

    <input type="text" id="', $key ,'" name="', $key ,'" value="', $_POST[$key], '" size="30"' ,'/>

    The error occurs on the value attribute on first load of the page, as there is no $_POST[$key] variable set. What is the easiest way to rectify this?
     
    Omzy, Jan 24, 2009 IP
  16. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #16
    isset($_POST[$key]) ? $_POST[$key] : ''
    PHP:
    etc.

    (And don't forget to validate.)
     
    Danltn, Jan 24, 2009 IP
  17. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Cheers mate. I've got another one which is erroring, this one is a little trickier though:

    $value[2] == $_POST['region_name'] ? ' selected="selected"' : '' ,'
    PHP:
    I cant figure out how to put the isset condition in there.
     
    Omzy, Jan 24, 2009 IP
  18. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #18
    joebert, Jan 25, 2009 IP
  19. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #19
    isset($_POST['region_name'], $value[2]) and $value[2] == $_POST['region_name'] ? ' selected="selected"' : ''
     
    Danltn, Jan 25, 2009 IP
  20. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Cheers again Danltn.

    That didn't quite work so I amended it as such:

    isset($_POST['region_name']) && $value[2] == $_POST['region_name'] ? ' selected="selected"' : ''

    I didn't realise you could combine an AND condition with a ternery operator. I'm getting there, slowly but surely...

    joebert - Thanks for the pointer. I'm currently developing and testing on my localhost server at home, so that's OK. But yeah I would never turn on error notices or output fatals on the live server! :)
     
    Omzy, Jan 25, 2009 IP