Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a function that returns an array and I want to check that a certain value is not set in the array.

function testFunction() {
    return array('key' => 404)
}

if (testFunction()['key'] < 404) {
    // stay calm and carry on
}
else {
    // uh oh
}

I understand that the above will not work in php 5.3 (or anything below), however, I'd like to know what I should do in the above case. That is - what should I call the temporary array I create for the if statement? Or is there another way of dealing with this situation?

The following is what I would roughly do currently:

function testFunction() {
    return array('key' => 404)
}

$test_array = testFunction();

if ($test_array['key'] < 404) {
    // stay calm and carry on
}
else {
    // uh oh
}

Is this an acceptable thing to do?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

So, .. you want to simply check whether a certain key in an array is set to a certain value.

You can't do this directly on the function call because older PHP versions don't support array dereferencing. You are right when saying you'll need an intermediate variable.

The only question remaining is what to name it. Considering that the variable will probably be situated in a really small scope, don't worry about it too much. There is a saying which says, the bigger the scope, the bigger the name. You could just name it $list.

Old replies:

Your code does something else as what you describe? If you want to see whether a given value is present in the array, use in_array().

UPDATE:

I believe I understand what you are trying to accomplish now. You just want to see whether the key is set?

array_key_exists()

PHP has got a pretty good online API, be sure to check it for existing implementations when looking for a solution.

share|improve this answer
    
According to the code, Jonathan also wants to compare against the value mapped to that key if there is one. –  David Harkness Mar 3 '11 at 2:44
    
I want to know if a specific key has the value 404. so in_array() will not work because the value may exist somewhere else and array_key_exist will not work because I know the key exists. I want to know if a specific key I know is there has the value 404. –  Jonathan Mayhak Mar 3 '11 at 2:56
    
@Jonathan Mayhak: K, I finally understand your question now. ;p I updated the answer. –  Steven Jeuris Mar 3 '11 at 3:21
    
I was just curious if there was some standard in php for dealing with this situation. Good to know what I've been doing is acceptable. –  Jonathan Mayhak Mar 3 '11 at 3:36

Your question does not match your code. You ask to check that a key does not exist in the array, yet you are actually testing the value mapped to that key against some other value, presumably doing something else if the key doesn't exist.

$result = testFunction();
if (!isset($result['key']) || $result['key'] < 404) {
    // ok
}
else {
    // panic
}

You can create a helper method with a suggestive name that does the full check and use that in your if clause.

function omgWeGotTheDreaded404() {
    $result = testFunction();
    return isset($result['key'] && $result['key'] >= 404;
}

...

if (omgWeGotTheDreaded404()) ...
share|improve this answer
    
Your answer is the same thing I wrote above for the second example. I know I can do what you wrote. I want to know if what we both wrote is the best way. What do you think is a good thing to name the temporary array? (you used $result and I used $test_array) –  Jonathan Mayhak Mar 3 '11 at 2:59
    
Steven's answer about the name is good advice. It lives such a short life, give it a terse name. My point is that I'd still put it inside a function that returns bool so you don't need a temporary array outside the function. If you only do this in one place, it's not of much value. –  David Harkness Mar 3 '11 at 8:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.