ACR Rules

  1. Home
  2. Docs
  3. ACR Rules
  4. Reliability (41)
  5. Check for empty string should be done properly

Check for empty string should be done properly

Introduced in version: 1.3 (29 Jan 2020)

‘This is very likely a bug and should be changed. There are only two correct ways to check for empty string

Noncompliant example:

  • $Str = empty and trim($Str) != '' – equal used instead of not equal or other way around
  • $Str = empty and trim($Str) = ''and is used instead of or or other way around
  • trim($Str) != ''and $Str != empty – the order of expression is reversed resulting in a null pointer exception

Compliant example:

Only two ways how to correctly check for empty string

  • if $Str != empty and trim($Str) != '' then $Str else 'empty'
  • if $Str = empty or trim($Str) = '' then 'empty' else $Str