2013-02-21 22:47:24 +04:00
Operators
=========
2013-07-22 18:03:43 +04:00
### Arithmetic operators
2013-02-21 22:47:24 +04:00
2013-07-22 18:03:43 +04:00
* `$a + $b` - addition
* `$a - $b` - subtraction
* `$a * $b` - multiplication
* `$a / $b` - division
* `$a % $b` - modulus
2013-02-21 22:47:24 +04:00
2013-07-02 11:07:33 +04:00
```smarty
{$a + $b * $c/$d - $e*5 + 1e3}
```
2013-02-21 22:47:24 +04:00
2013-07-22 18:03:43 +04:00
### Logical operators
2013-02-21 22:47:24 +04:00
2013-07-22 18:03:43 +04:00
* `$a || $b` - or
* `$a && $b` - and
* `!$a` - not, unary operator
* `$a and $b` - and
* `$a or $b` - or
* `$a xor $b` - xor
2013-02-21 22:47:24 +04:00
2013-07-02 11:07:33 +04:00
```smarty
2013-07-22 18:03:43 +04:00
{if $b & & $c} ... {/if}
2013-07-02 11:07:33 +04:00
```
2013-02-21 22:47:24 +04:00
2013-07-22 18:03:43 +04:00
### Comparison operators
2013-02-21 22:47:24 +04:00
2013-07-22 18:03:43 +04:00
* `$a < $b` - less than
* `$a > $b` - greater than
* `$a <= $b` - less than or equal to
* `$a >= $b` - greater than or equal to
* `$a == $b` - equal
* `$a === $b` - identical
* `$a !== $b` - not identical
* `$a != $b` - not equal
* `$a <> $b` - not equal
2013-02-21 22:47:24 +04:00
2013-07-02 11:07:33 +04:00
```smarty
2013-07-22 18:03:43 +04:00
{if $b >= 5} ... {/if}
2013-07-02 11:07:33 +04:00
```
2013-02-21 22:47:24 +04:00
2013-07-22 18:03:43 +04:00
### Bitwise operators
2013-07-02 11:07:33 +04:00
2013-07-22 18:03:43 +04:00
* `$a | $b` - or
* `$a & $b` - and
* `$a ^ $b` - xor
* `~$a` - not, unary operator
* `$a << $b` - shift left
* `$a >> $b` - shift right
2013-07-02 11:07:33 +04:00
```smarty
2013-07-22 18:03:43 +04:00
{if $a & 1} {var $b = 4 | $flags} {/if}
2013-07-02 11:07:33 +04:00
```
2013-07-22 18:03:43 +04:00
### Assignment Operators
2013-07-02 11:07:33 +04:00
2013-07-22 18:03:43 +04:00
* `$a = $b` - assignment
* `$a += $b` - assignment with addition
* `$a -= $b` - assignment with subtraction
* `$a *= $b` - assignment with multiplication
* `$a /= $b` - assignment with division
* `$a %= $b` - assignment with modulus
* `$a &= $b` - assignment with bitwise And
* `$a |= $b` - assignment with bitwise or
* `$a ^= $b` - assignment with bitwise xor
* `$a <<= $b` - assignment with left shift
* `$a >>= $b` - assignment with right shift
```smarty
{var $b |= $flags}
```
### Incrementing/Decrementing operators
* `++$a` - increment the variable and use it
* `$a++` - use the variable and increment it
* `--$a` - decrement the variable and use it
* `$a--` - use the variable and decrement it
2013-08-03 18:56:17 +04:00
### String operator
* `$a ~ $b` - return concatenation of variables `$a` and `$b`
2013-07-22 18:03:43 +04:00
### Ternary operator
* `$a ? $b : $c` - returns `$b` if `$a` is not empty, and `$c` otherwise
* `$a ! $b : $c` - returns `$b` if `$a` is set, and `$c` otherwise
* `$a ?: $c` - returns `$a` if `$a` is not empty, and `$c` otherwise
* `$a !: $c` - returns `$a` if `$a` is set, and `$c` otherwise
2013-07-02 11:07:33 +04:00
```smarty
{var $a = true}
{$a ? 5 : 10} {* outputs 5 *}
{var $a = false}
{$a ? 5 : 10} {* outputs 10 *}
```
2013-07-22 18:03:43 +04:00
### Check operator
2013-07-02 11:07:33 +04:00
2013-07-22 18:03:43 +04:00
* `$a?` - returns `TRUE` if `$a` is not empty
* `$a!` - returns `TRUE` if `$a` is set
2013-07-02 11:07:33 +04:00
```smarty
2013-07-22 18:03:43 +04:00
{if $a?} {* instead of {if !empty($a)} *}
2013-07-02 11:07:33 +04:00
{if $a!} {* instead of {if isset($a)} *}
{$a?:"some text"} {* instead of {if empty($a) ? "some text" : $a} *}
2013-07-22 18:03:43 +04:00
{$a!:"some text"} {* instead of {if isset($a) ? $a : "some text"} *}
2013-07-02 11:07:33 +04:00
```
2013-07-22 18:03:43 +04:00
### Test operator
Tests can be negated by using the `is not` operator.
* `$a is $b` - $a identical $b
* `$a is integer` - test variable type. Type may be int/integer, bool/boolean, float/double/decimal, array, object, scalar, string, callback/callable, number/numeric.
* `$a is iterable` - test variable for iteration.
* `$a is template` - variable `$a` contain existing template name.
* `$a is empty` - checks if a variable is empty.
* `$a is set` - checks if a variable is set.
* `$a is even` - variable `$a` is even.
* `$a is odd` - variable `$a` is odd.
* `$a is MyClass` or `$a is \MyClass` - variable `$a` instance of `MyClass` class
### Containment operator
Tests can be negated by using the `not in` operator.
* `$a in $b` - variable `$a` contains in `$b` , $b may be string, plain or assoc array.
* `$a in list $b` - variable `$a` contains in array `$b` as value
* `$a in keys $b` - variable `$a` contains in array `$b` as key
* `$a in string $b` - variable `$a` contains in string `$b` as substring
2013-07-02 11:07:33 +04:00
```smarty
2013-07-22 18:03:43 +04:00
{'df' in 'abcdefg'}
{5 in [1, 5, 25, 125]}
{2 in keys [1, 5, 25, 125]}
```