0

I'm having a problems with making if statements:

<?php if (isset($detail) == 1) { ?>
    // code if detail variable exists and is equal to 1
<?php } else { ?>
    // code if detail variable doesn't exist or is not equal 1
<?php }?>

For some reason this condition doesn't work. How could I fix the if condition, so it will work?

adamdehaven
  • 5,890
  • 10
  • 61
  • 84
Derfder
  • 3,204
  • 11
  • 50
  • 85

1 Answers1

2

isset is a boolean operator which returns TRUE or FALSE. You don't need to compare it to 1.

if( isset($detail) )

As a side note, just because the variable is set, does not mean it's not empty. You may want to just check that it's not empty:

if( !empty($detail) )
Kermit
  • 33,827
  • 13
  • 85
  • 121
  • aha so this could be the problem, becasue I am sending something like this `$this->data['detail'] = 0;` or `$this->data['detail'] = 1;` – Derfder Mar 27 '13 at 20:47
  • So, should I use rather empty too? Could it be done even simpler? I mean any function in php for this "double check"? – Derfder Mar 27 '13 at 20:48
  • if it doesn't exist I will do my helper, but it would be neat if it already exists in php – Derfder Mar 27 '13 at 20:49
  • @Derfder You can just use `empty`. [Read more here](http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty). – Kermit Mar 27 '13 at 20:50
  • So e.g. if the variable $detail is not sent at all to the view, no php error will pop up? Is using empty enough to prevent nasty php errors caused by not sending variable to the view? – Derfder Mar 27 '13 at 20:54
  • `empty` will return `true` if the value is `''`, `0.0`, `'0'`, `0`, `FALSE`, `array()`. Do you want that? – Michael Mar 27 '13 at 21:01
  • is this better than `!empty`?: `if(isset($detail) && $detail === TRUE)` – Derfder Mar 27 '13 at 21:04