Reputation: 109
$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.
Upvotes: 0
Views: 146
Reputation: 1174
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!
Upvotes: 0
Reputation: 7025
Use isset()
for the condition and $_POST['item1']
after the question mark.
$myitem = isset($_POST['item1']) ? $_POST['item1'] : NULL;
Upvotes: 9