Twig expression operator

Tags:

Expressions can be used in {% blocks %} and ${ expressions }.

Operator Description
== Does the left expression equal the right expression?
+ Convert both arguments into a number and add them.
- Convert both arguments into a number and substract them.
* Convert both arguments into a number and multiply them.
/ Convert both arguments into a number and divide them.
% Convert both arguments into a number and calculate the rest of the integer division.
~ Convert both arguments into a string and concatenate them.
or True if the left or the right expression is true.
and True if the left and the right expression is true.
not Negate the expression.

PHP double quotes vs single quotes
By Virendra Chandak on March 18, 2012 Leave a comment Go to comments (15)

Strings in PHP can be specified in four different ways: single quoted, double quoted, heredoc syntax and (since PHP 5.3.0) nowdoc syntax, the first two of them being by far the most frequently used.

It is important to know the difference between using single quotes and double quotes. In this post we will see the difference between them and which should be used when.

Single quoted strings are the easiest way to specify string. This method in used when we want to the string to be exactly as it is written. When string is specified in single quotes PHP will not evaluate it or interpret escape characters except single quote with backslash (‘) and backslash(\) which has to be escaped.
1
2

echo 'This is \'test\' string';
//Output: This is 'test' string

In double quoted strings other escape sequences are interpreted as well any variable will be replaced by their value.
1
2
3

$count = 1;
echo "The count is $count";
//Output: The count is 1

If we use single quotes instead of double quotes for the above example it will be like this:
1
2
3

$count = 1;
echo 'The count is $count';
//Output: The count is $count

I recommend using single quotes (‘ ‘) for string unless we need the double quotes (” “). This is because double quotes forces PHP to evaluate the string (even though it might not be needed), whereas string between single quotes is not evaluated. Also, parsing variables between strings takes more memory than concatenation.

So instead of this:
1
2

$count = 1;
echo "The count is $count";

use this:
1
2

$count = 1;
echo 'The count is ' . $count;