18

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.

Here's the if statement:

 if ($products[$_POST['product']] == $_POST['product']) {
do everything;}
else {
echo "This item is not available";
}

EDIT2:

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.

This is the full script:

<?php

session_start();

//Getting the list
 $_SESSION['list'] = isset($_SESSION['list']) ? $_SESSION['list'] : array();    

 //stock    
 $products = array(      
     'Pineaple' => 500, 'Banana' => 50, 'Mango' => 150,       
     'Milk' => 500, 'Coffe' => 1200, 'Butter' => 300,      
     'Bread' => 450, 'Juice' => 780, 'Peanuts' => 800,      
     'Yogurt' => 450, 'Beer' => 550, 'Wine' => 2500,    
 );    

if( isset($_POST['product']) ){


     //Saving the stuff    
     $new_item = array(      
         'item' => $_POST['product'],       
         'quantity' => $_POST['quantity'],     
         'code' => $_POST['code'],      
         'price' => $products[$_POST['product']] * $_POST['quantity'],    

     );



    $new_product = true;    
    foreach($_SESSION['list'] as $key => $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  "<b>SHOPPING LIST</b></br>";    
    foreach($_SESSION['list'] as $key => $item) {       
        echo 'Product .'. $key. ' '. $item['item'], ' ', $item['quantity'], ' units: ', $item['price']. '<br />';    
        }

}

else {
echo "This item is not available";
}

echo "</br> <a href='index.html'>Return to index</a> </br>";

//Printing session
var_dump($_SESSION);

session_destroy();

?>
2

10 Answers 10

34

With PHP 7, the null coalescing operator can be used to deal with optional request variables. You can change your $_POST['product'] reference to $_POST['product'] ?? null which will resolve to null (rather than throwing the warning) if 'product' is not a valid key in the post array. If you wanted to check both the $_POST and $_GET arrays for a value, you would use $_POST['product'] ?? $_GET['product'] ?? null.

Sign up to request clarification or add additional context in comments.

3 Comments

Upvote for referencing the new(ish) operator. It's more declarative and shorter.
This should be the best answer, it's definitively faster to type and the answer author highlighted its use in this context that's not so obvious.
Yes, I love this.
26

I'm a bit confused by your code. It looks like your array has the same key and value, so:

$products['saucepan'] = 'saucepan'

Perhaps you are trying to do this, which will check whether the product exists in the products array:

if(isset($_POST['product']) && array_key_exists($_POST['product'], $products))
{
  // do stuff
}
else
{
  echo "This item is not available";
}

1 Comment

+1 for array_key_exists, isset's more specific, more useful big brother.
12
@$_POST['product']

(with an @) will return the same thing as :

$product = (isset($_POST['product'])) ? $_POST['product'] : null;

Shorter is sweeter !! 4 times less code ! Easier to read and understand.

The @ symbol is the error control operator (AKA the "silence" or "shut-up" operator). It makes PHP suppress any error messages (notice, warning, fatal, etc.).

But beware not to use @ for any other thing, as it would make your code so much harder to debug!

(this will answer your question before the edit)

6 Comments

This doesn’t seem like a good answer. Silencing errors is never the right approach.
@EvanHendler checking for isset first then evaluing the value is no better than the ' @ ' ! But for other things, silencing errors isn't a great thing to do - that is especially why I warn people about it in the answer.
I've gotta go with Cedric on this one, the @ operator has a very limited use, up til now I have used it only on @session_start() (which throws an annoying notice for apparently no functional reason), the succinctness of $var = @$_POST['var'] is way too nice to throw it in the 'never suppress errors' basket.
@EvanHendler Silencing errors is never right approach, but silencing notices is a different story. Notices might help with finding cause of follow-up error but if my code is fine with getting null instead of expected value this mandatory array_key_exists() might pollute code in PHP quite extensively making it harder to comprehend.
man! you just made my day, i've been looking for this thing for a log time.
|
9

You should first check to see if $_POST['product'] is set with isset(), like:

if( isset($_POST['product']) ){
    // Do something with $_POST['product']
}

That should suppress the warning.

Comments

3

You could also try

$product = (isset($_POST['product'])) ? $_POST['product'] : null;

This will set $product to the $_POST value if it exists, or to null if not. Then you could try

if ($product) {
  do_something();
}

or access your array with

$products[$product];

I feel this way it makes your code that little bit easier on the eyes..

Comments

1

You can just use a ?? to set set a default value if your value is not set.

$_POST['product'] ?? "No product";

Comments

1

I think this is the most elegant and readable way of doing it, it is not a one-liner but readable nevertheless.

$product = $_POST['product'] ?? null;
if(empty($product)){
 // $product was not set in the request. Interrupt, throw error or deal with it here
}

// At this point $product is set, continue as normal

Comments

0
if (isset($products[$_POST['product']]) && $products[$_POST['product']] == $_POST['product']) 

the isset() function will help you avoid the warning

Comments

0
if(isset($products[$_POST['product']) && $products[$_POST['product'] != "")

Note that a vriable can be empty but also "set" so the what comes after the && is necessary.

4 Comments

You could use if(empty($products[$_POST['product'])) which will also check whether the value is null or 0.
@Blowski : if index 0 valid for $product then?
@Gaurav Fair point. I would check the $_POST['product'] first and then use array_key_exists() as in my answer.
Thanks fatnjazzy, however since I'm using a SESSION the invalid value will be re-used, thus stopping the event would be much more effective, can you check for a minute my recently EDITED question?
0

you can check whether the index 'product' is defined in the same if statement ..

if (isset($_POST['product']) && $products[$_POST['product']] == $_POST['product']) {
    do everything;
}
else {
    echo "This item is not available";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.