title: PHP: Avoid undefined index?
body: <p>Every time a POST value is not equal to the list of values set in an array will return: Undefined Index error, I made an if statement but is not working.</p>
  
  <p>Here's the if statement:</p>
  
  <pre><code> if ($products[$_POST['product']] == $_POST['product']) {
  do everything;}
  else {
  echo "This item is not available";
  }
  </code></pre>
  
  <p><strong>EDIT2:</strong></p>
  
  <p>Seen the current situation avoiding the warning wont help much because I'm dealing with several factors, for example a list of items in a Shopping Cart, if the invalid product is not removed, it will be added to the shopping list session.</p>
  
  <p>This is the full script:</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  //Getting the list
   $_SESSION['list'] = isset($_SESSION['list']) ? $_SESSION['list'] : array();    
  
   //stock    
   $products = array(      
       'Pineaple' =&gt; 500, 'Banana' =&gt; 50, 'Mango' =&gt; 150,       
       'Milk' =&gt; 500, 'Coffe' =&gt; 1200, 'Butter' =&gt; 300,      
       'Bread' =&gt; 450, 'Juice' =&gt; 780, 'Peanuts' =&gt; 800,      
       'Yogurt' =&gt; 450, 'Beer' =&gt; 550, 'Wine' =&gt; 2500,    
   );    
  
  if( isset($_POST['product']) ){
  
  
       //Saving the stuff    
       $new_item = array(      
           'item' =&gt; $_POST['product'],       
           'quantity' =&gt; $_POST['quantity'],     
           'code' =&gt; $_POST['code'],      
           'price' =&gt; $products[$_POST['product']] * $_POST['quantity'],    
  
       );
  
  
  
      $new_product = true;    
      foreach($_SESSION['list'] as $key =&gt; $item) {      
          if ($item['item'] == $new_item['item']) {        
          $_SESSION['list'][$key]['quantity'] += $new_item['quantity'];        
          $_SESSION['list'][$key]['price'] = $products[$new_item['item']] * $new_item['quantity'];        
          $new_product = false;
          }    
      }   
  
      if ($new_product) {      
          $_SESSION['list'][] = $new_item;        
      }    
  
      /*if ($new_item['item'] != $products[$new_item['item']]) {
          echo "This item is not available";}*/
  
      //listing    
      echo  "&lt;b&gt;SHOPPING LIST&lt;/b&gt;&lt;/br&gt;";    
      foreach($_SESSION['list'] as $key =&gt; $item) {       
          echo 'Product .'. $key. ' '. $item['item'], ' ', $item['quantity'], ' units: ', $item['price']. '&lt;br /&gt;';    
          }
  
  }
  
  else {
  echo "This item is not available";
  }
  
  echo "&lt;/br&gt; &lt;a href='index.html'&gt;Return to index&lt;/a&gt; &lt;/br&gt;";
  
  //Printing session
  var_dump($_SESSION);
  
  session_destroy();
  
  ?&gt;
  </code></pre>