html - PHP Check if var is 0 or 1 and then print accordingly

976

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)

See the .

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).

People are also looking for solutions to the problem: php - Is there a faster/better way to check if an URL is reachable than cURL?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.