17

I want to check if:

  • a field in the array isset
  • the field === true

Is it possible to check this with one if statement?

Checking if === would do the trick but a PHP notice is thrown. Do I really have to check if the field is set and then if it is true?

Mat
  • 202,337
  • 40
  • 393
  • 406
Sebastian Sebald
  • 16,130
  • 5
  • 62
  • 66

4 Answers4

32

If you want it in a single statement:

if (isset($var) && ($var === true)) { ... }

If you want it in a single condition:

Well, you could ignore the notice (aka remove it from display using the error_reporting() function).

Or you could suppress it with the evil @ character:

if (@$var === true) { ... }

This solution is NOT RECOMMENDED

Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
2

I think this should do the trick ...

if( !empty( $arr['field'] ) && $arr['field'] === true ){ 
    do_something(); 
}
askome
  • 83
  • 1
  • 1
  • 6
  • I think the point is kinda have it in a single condition... But maybe that's what he meant and I misunderstood. – Madara's Ghost Oct 26 '11 at 20:04
  • He said one IF statement, that is one. Having && doesn't make it two, does it? If so, then I guess I was wrong. Didn't come to what you replied tho', clever :) – askome Oct 26 '11 at 20:06
  • My problem was that the notice was thrown when I checked in a single statement. But the @ will Do the trick I think. I thought I was doing something wrong :-/ – Sebastian Sebald Oct 26 '11 at 20:43
  • empty() and inset() respectively will take that error away, well at least it does for me. However @ is shorter and sweeter indeed. Best of luck for you future ventures :) – askome Oct 26 '11 at 20:47
2

Alternative, just for fun

echo isItSetAndTrue('foo', array('foo' => true))."<br />\n";
echo isItSetAndTrue('foo', array('foo' => 'hello'))."<br />\n";
echo isItSetAndTrue('foo', array('bar' => true))."<br />\n";

function isItSetAndTrue($field = '', $a = array()) {
    return isset($a[$field]) ? $a[$field] === true ? 'it is set and has a true value':'it is set but not true':'does not exist';
}

results:

it is set and has a true value
it is set but not true
does not exist

Alternative Syntax as well:

$field = 'foo';
$array = array(
    'foo' => true,
    'bar' => true,
    'hello' => 'world',
);

if(isItSetAndTrue($field, $array)) {
    echo "Array index: ".$field." is set and has a true value <br />\n";
} 

function isItSetAndTrue($field = '', $a = array()) {
    return isset($a[$field]) ? $a[$field] === true ? true:false:false;
}

Results:

Array index: foo is set and has a true value
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
0

You can simply use !empty:

if (!empty($arr['field'])) {
   ...
}

This is precisely equivalent to your conditions by DeMorgan's law. From PHP's documentation, empty is true iff a variable is not set or equivalent to FALSE:

  isset(x) && x
  !(!isset(x) || !x)
  !empty(x)

As you can see, all three of these statements are logically equivalent.

alexw
  • 8,468
  • 6
  • 54
  • 86