0
$myitem = $_POST['item1'] ? myitem : NULL ;

is this possible? I got an error Notice: Undefined index

I use conditioning number of object items by ajax. for example sometime $_POST['item1'] is not passed.

0

2 Answers 2

9

Use isset() for the condition and $_POST['item1'] after the question mark.

$myitem = isset($_POST['item1']) ? $_POST['item1'] : NULL;
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! Did my answer help you? I would appreciate if you could accept the answer if it did. Thank you!
0

I suggest that you use array_key_exists() in place of isset() as the latter one checks for the value also! For example, if the user enter's the number '0' in a field, isset() and empty() will return false while array_key_exists() will return true.

Your code becomes:

$myitem = array_key_exists($_POST['item1']) ? $_POST['item1'] : NULL ;

This is a mistake that most new PHP developers make and it hides in plain sight, giving them nightmares!

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.