7

Is it possible to check the value of a certain key in a PHP array in 1 IF statement? Right now, in order to not throw an index offset error I have to check if the key is set, then check its value.

if (isset($array[$key]))
{
    if ($array[$key] == $x)
    {
        // do stuff
    }
}

(sorry, accidentally put ! in first IF originally)

3
  • where is the code checking value? Commented Apr 6, 2011 at 17:55
  • 1
    ok. if it is not set, then u cant check its value... Commented Apr 6, 2011 at 17:56
  • if ( isset($array[$key]) && $array[$key] == $x) { // do stuff } Commented Apr 6, 2011 at 17:56

6 Answers 6

7

The && operator is short-circuit, thus:

if (isset($array[$key]) && $array[$key] == $x)
    // do stuff
}

Happy coding.

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

3 Comments

This will have errors. if it is not set than u cannot check if it is equal to something... see my answer
@Neal Thanks, but with respect, please see the comment "...ADJUST to make it correct". The short-circuit nature of && still holds, this is an exercise for the poster ;-)
thanks, i didn't know && was short-circuited. "AND" is also short-circuited, I believe.
3

You may also use a reference: if $array[$key] does not exist, then it will be created and set to null; therefore, no error will occur. This is most useful when you expect the value to exist; ie, you do not want to act specially if the value does not.

if (&$array[$key] == $x) {

}

Comments

2

try this. ur current code wont do anything bc if it is not set, the second if statement will never be...

if (isset($array[$key]) && $array[$key] == $x)
{
    //do stuff if that key == $x
}

Comments

1

Yes, with the boolean operator && ;)

if (isset($array[$key]) && ($array[$key] == $x)) {
  // do stuff
}

2 Comments

This will have errors. if it is not set than u cannot check if it is equal to something... see my answer
Took the code from the question and didnt verified it. Of course, I should have done it.
1

Best approach is using array_key_exists() function.

An example- array_key_exists($key,$array);

Check the documentation link more details.

Comments

0

if you have an associative array compare the size of array to the key index instead of the $key

if(sizeof($array) >= $key){

   if ($array[$key] == $x)
    {
    // do stuff
    }

}

4 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
I think you posted this to the wrong answer mate. My answer is made of code and never had any links!
Appolgies the text was auto generated. The answer is code only and should contain some thing else to describe what's happening
The description was wrapped between "/* ... */" in the code itself. I took it of the code. Cheers

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.