Skip to content

Documentation for if has innacurate/incomplete edge cases #2677

@ismael-miguel

Description

@ismael-miguel

Currently, there's the following list of edge cases, on https://twig.symfony.com/doc/2.x/tags/if.html

edge cases

This is innacurate/incomplete, as these edge cases have edge cases.

These are my findings:

  • empty string: false
  • numeric zero: false
  • whitespace-only string: false?
    Running echo !!' '; shows 1
    Running echo ' ' == 0; shows 1
    Running echo ' ' == '0'; doesn't display anything
    The whitespace-only string (or any string) will be casted to a number when compared for equality with a number. Otherwise, everything is casted to a string.
  • empty array: false
    Everything is casted to an array when comparing to an empty array, except boolean values. In that case, the empty array is casted to the boolean value (false)
  • null: false
    Null is a special value. Null will cast to a string when comparing with a string, to a number when comparing to a number but won't be cast to an empty array when comparing to an empty array.
    My guess is due to the fact that you can write function xyz(array $arg = null){ [...] }, to distinguish between an empty array and a function with an optional parameter.

Everything below is correct.

You can see it in action on http://sandbox.onlinephpfunctions.com/code/e4dc15a4f29cd6ef6a7498ad27ac202f5f874612

Should this information be added to the documentation? Is it fine the way it is?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions