html - PHP Check if var is 0 or 1 and then print accordingly
Solution:
$myvalue= '';
if (!empty($final_data['esea'])) {
$myvalue= 'Foo'
} else {
$myvalue= 'Bar'
}
echo $myvalue;
If the output isFoo
then$final_data['esea']
was one of:
- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
If the output isBar
then$final_data['esea']
was none of the above values.
Also see the comments here and here.
Also note that the above code can be rewritten to:
$myvalue= '';
if (empty($final_data['esea'])) {
$myvalue= 'Bar'
} else {
$myvalue= 'Foo'
}
echo $myvalue;
Which is functionally exactly the same. The only difference isemtpy(...)
v.s.!empty(...)
(and ofcourse the logic in the if/else swapped).