tags:

views:

215

answers:

5

Is there a better (ie, more readable) way to write this?

if (isset($input_vars['directive']) && $input_vars['directive'] == 'edit') {
+4  A: 

Not really, unfortunately. You could wrap this code in a function and simply call that every time you need this.

function compareArrayItem($array, $key, $value) {
    return isset($array[$key]) && $array[$key] == $value;
}

if (compareArrayItem($input_vars, 'directive', 'edit')) {
    // Do something
}

But that seems kind of pointless to me (and less readable than your original code). Or you could lower the error reporting level to not include E_NOTICE so that you don't need the first expression at all.

error_reporting(E_ALL ^ E_NOTICE);

if ($input_vars['directive'] == 'edit') //...

But I wouldn't recommend doing this just for the sake of shortening your code.

If I were you, I'd just leave it alone. It's fine as-is.

musicfreak
`$input_vars` wraps `$_POST` and `$_GET`, so it can't just "be set earlier." I don't know why the original devs did this, and didn't just use `$_REQUEST`.
gms8994
@gms8994: Then just leave it alone. It looks fine and readable to me. It might be a bit longer than is ideal, but that's not something to fret about, IMO. You might want to edit your question to include that information, though, because that's important...
musicfreak
A: 

The following would yield the same result every time.

if($input_vars['directive'] == 'edit'){

This is because if its not set then its not 'edit', if its 'edit' then its set.

This does return a notice but you may turn it that feature off (Not saying that you should) from your PHP installation.

Jonathan Czitkovics
Isn't this going to spit out errors of that value isn't set at all?
Austin Fitzpatrick
You'll get a notice about a non-existant index if the directive key isn't set.
Simon
I just tried it and its because my system has notices turned off. You could do that too if you want.
Jonathan Czitkovics
I almost downvoted this for the notice errors, but then I realized that it makes more sense to always initialize the variables that you are going to use. You won't have that problem *and* your code becomes more readable.
Syntax Error
@Syntax Error this is true unless your testing for `$_POST` or `$_GET` but this is not the case.
Jonathan Czitkovics
True, I should have mentioned that. Also there are other cases where you might use $$var or something and need to check.
Syntax Error
@Jonathan - This actually wraps $_POST/$_GET in to a special array... Don't yell at me, I didn't write it ;)
gms8994
I strongly discourage from turning notice level off. If you do that then you'll end up with "code checked only with your eyes" :-)
MartyIX
IMHO it's silly to show all errors in PHP, it's best to only show critical errors and depreciated warnings. PHP is a loosely typed language and you shouldn't worry about scenarios when a variable is unset, it's what makes PHP easy, embrace it.
TravisO
OMG such many people have no idea of debugging. When maxed error reporting level is **huge** help. I bet most of them have no idea where the error log on their system is. That's PHP's fate...
Col. Shrapnel
I can't believe you are voting this! Turning off notices? It's the stupidest advice I've ever heard!
treznik
Turning notices off for the sake of readability !!?? Just initialize the variable first- no big deal
phpfour
Sorry I should have been more specific. I was not saying that notices should be turned off. I'm just saying that you can turn it off if you want. I personally would not use it but I have to develop this way because of where I work. Also as specified by someone else you may also initialize the variable before testing it.
Jonathan Czitkovics
A: 

I'd say:

if(@$input_vars['directive']=='edit') {

Would be slightly more readable and it doesn't produce warnings if directive isn't there.

Daniel
this isn't recommended. Silent warnings by using *@* can make the code too hard to follow. Also the overhead just for doing is a lot
Gabriel Sosa
A: 

I'm going to guess that you'll be testing $input_vars['directive'] against more than one value (otherwise, why would you not just have a simple boolean stored in $input_vars['edit'] or similar?). I would also hazard a guess that you're doing those tests one after the other (if 'edit' do X, else if 'display' do Y).

In such a case, just put the isset() test in an if statement and nest the others inside that (switch/case flow wouldn't be a bad choice).

pdehaan
+1  A: 

If the set of allowed values in $input_vars is known and the checks you mention are all over the place the following helper function will do:

function item ($array, $key) {
    if (isset ($array [$key]))
        return $array [$key];
    else
        return NULL; // Or use whatever is more appropriate
}

Then the original code can be changed to look like

 if (item ($input_vars, 'directive') == 'edit') { ...

This is not only more readable, but also removes duplication: both array variable and key appear only once.

kwaxer