null null - 1 year ago 97
PHP Question

Fixing the PHP empty function

PHP has the habit of evaluating (int)0 and (string)"0" as empty when using the

function. This can have unintended results if you expect numerical or string values of 0. How can I "fix" it to only return true to empty objects, arrays, strings, etc?

Answer Source

This didn't work for me.

if (empty($variable) && '0' != $variable) {
  // Do something

I used instead:

if (empty($variable) && strlen($variable) == 0) {
  // Do something