phpsmartysmarty2

Why is this Smarty if statement always evaluating true?


This:

{1234|count_characters}

Outputs:

4

Good. But this:

{if 12345|count_characters == 4}
    it's 4
{/if}

outputs it's 4. The above if statement should evaluate to false obviously. Why is it always evaluating true?

The count_characters smarty modifier simply shows the number of characters in a string or number:

http://www.smarty.net/docsv2/en/language.modifier.count.characters.tpl


Solution

  • Because the {x|y} syntax is a smarty shortcode for echo y(x).... where echo is significant

    However, {if y|x ==4 } is the equivalent of performing bitwise or (|) on x and y (12345|count_characters is true because 12345 is true, even though count_characters would be tested as a constant and if the constant doesn't exist then as a string literal, while throwing a notice), comparing the result of that operation against 4 (true == 4 with loose-typing), and then if true, etc