D. Joe
D. Joe

Reputation: 9

Check if variable has value and isn't empty

I want to check if a numeric variable has a value (including '0') and is not empty. Empty meaning EMPTY (''), not '0'.

Is this really the best I can do with PHP?

if (isset($variable) && $variable !== '') { ... }

I'd like to do this with one check without writing a function for it...

Upvotes: 0

Views: 3019

Answers (4)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

(The answer has been edited. Consult the additionals further down under "ternary operations").

Why go through the trouble of using all that?

Just use an "not empty" if(!empty($var)){...}

However, if you're using this with a GET array, then yes; it would be best to use an isset() and empty() on a conditional statement.

I want to check if a variable has a value (including '0') and is not empty

That to me interprets as:

Check if a value has a value and is not empty (as you wrote) and stands to contain a 0 (zero).

Therefore:

if(!empty($var) && $var='0'){...}


I'd like to do this with one check without writing a function for it...

Use a ternary operator then.

However "without a function"... right well you can't. You still need "some type of function".

About that "ternary operator" I mentioned above. You can reference what are called "nested ternary operations" in both these Q&A's on Stack:

That way you won't need a custom function.

  • Sidenote: I am by far not taking away or trying to take away from (Charlotte's) accepted answer (which should remain as accepted). This is just an additional method of achieving your (ultimate) goal.

Upvotes: 1

Ray
Ray

Reputation: 41428

Yes, your way is the best (most efficient) way to:

  • insure the variable has been set (so you don't get an warning checking a variable that's not been set)
  • it's not the empty string ''
    • But, could be '0', 0,false, null, or [] which all count as empty in php, but you wish to consider as non-empty as indicated by your OP
    • your !== will ensure only exactly the string '' is compared (no casting/conversion)

The use of strlen works as well, but if you look at the opcode generated you'll see direct comparison is more 3 times computationally more efficient (assuming all operations are equally weighted, even more efficient if operations like DO_FCALL take significantly more cycles to execute than a basic IS_NOT_IDENTICAL check)

The !== ''version bytecode:

    IS_NOT_IDENTICAL                                     ~1      !0, ''

The strlen() > 0 version bytecode:

     SEND_VAR                                                 !0
     DO_FCALL                                      1  $1      'strlen'
     IS_SMALLER                                       ~2      $1, 0

Upvotes: 2

Charlotte Dunois
Charlotte Dunois

Reputation: 4680

The best thing you could do, is making your own custom function. The point is to pass the variables by reference to not trigger a warning, when you pass an undefined variable. As posted as comment, I'd use something along the line isset($variable) AND !empty($variable) AND !is_numeric($variable) AND $variable !== false to cover all cases.

Your custom function could look like this (improved version):

function is_blank(&$variable) {
    return (bool) !(isset($variable) AND (!empty($variable) OR is_numeric($variable) OR $variable === false));
}

https://3v4l.org/ZcCDu

Upvotes: 2

Jeremy Harris
Jeremy Harris

Reputation: 24549

What you are trying to check is string length, not "empty". This can easily be done using strlen().

if (isset($variable) && strlen($variable) > 0) {
    // Do something
}

If you want to exclude whitespace as invalid, you can add a trim() in there as well (generally recommended).

if (isset($variable) && strlen(trim($variable)) > 0 } {
    // ...
}

Upvotes: 2

Related Questions