1

From what I've read, if statement conditionals should break as soon as a false is found however:

if(array_key_exists('cool', $_POST) && $_POST['cool'] == 1)

returns an index undefined error. What I want to do is check to see if the key is even there and then check it's value, but the only way I've been able to do that is:

if(array_key_exists('cool', $_POST)) {
  if($_POST['cool'] == 1)

and that means I have to have multiple else blacks as well. Is there anyway to do this with less code?

1
  • 1
    Use isset() Commented Jan 23, 2012 at 17:49

3 Answers 3

2

You can use isset which is a language construct as opposed to a function call:

if(isset($_POST['cool']) && $_POST['cool'] == 1)
Sign up to request clarification or add additional context in comments.

Comments

1

You need to check if the key exists in the array:

if(array_key_exists('cool',$_POST) && $_POST['cool'] == 1)

or use isset().

1 Comment

isset fixed it. Thank you. I must have been mistyping it.
1

Try this

if(array_key_exists('cool',$_POST) && $_POST['cool'] == 1)

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.