php - checking if a string variable has data in it, which one is better !empty() or '!' -


It happened to me that some people empty ($ val) more $ Val < / Code> to check a string existence! What are the benefits of using empty () more than ! if any? Assume that the string can be:

  1. ""
  2. "false"
  3. "1"
  4. "0"

All of the above give the same results in both methods. Is there any other case that will cause problems in using one instead of the other?

empty ($ var) versus ! $ Var aka $ var == incorrect

The only difference is that empty one undefined Variable notice if the variable is not present, otherwise they are both the same. Now, if you are certain that the variable should is present, then just ! Use if the variable is not legally present , use empty if you use the idle empty , You are only disabling PHP's error reporting mechanism that can help you catch problems with mistyped variables or logic errors.

Also see.


Comments